Creating Directory or Folder using ASP.NET in C#

Creating a directory is very important when you are running a community website where user posts articles, forum messages etc. When a user registers in a web site a folder can be created dynamically then when you upload or ask a query, the data can be automatically placed inside the user's own directory instead of the root or anywhere else. Creating directory is a very easy task; we just require the namespace System.IO and a couple lines of code. Here you go:

ASP.NET

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title
>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <br /><br />
        <asp:Button ID="Button1" runat="server" Text="Create New Directory"
            onclick="Button1_Click" />
        <br /><br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form
>
</body>
</
html>

C# Code

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.IO; //additional namespace is required for io operations

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void CreateDirectoryIfNotExists(string NewDirectory)
    {
        try
        {
            // Checking the existance of directory
            if (!Directory.Exists(NewDirectory))
            {
                //If No any such directory then creates the new one
                Directory.CreateDirectory(NewDirectory);
                Label1.Text = "Directory Created";
            }
            else
            {
                Label1.Text = "Directory Exist";
            }
        }
        catch (IOException _err)
        {
            Response.Write(_err.Message);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string NewDirectory = Server.MapPath("Abhimanyu Kumar Vatsa");
        //New Directory Name in string variable
        CreateDirectoryIfNotExists(NewDirectory);
        //Calling the function to create new directory
    }
}


VB Code

Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Configuration
Imports System.IO
'additional namespace is required for io operations
Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Private Sub CreateDirectoryIfNotExists(ByVal NewDirectory As String)
        Try
            ' Checking the existance of directory
            If Not Directory.Exists(NewDirectory) Then
                'If No any such directory then creates the new one
                Directory.CreateDirectory(NewDirectory)
                Label1.Text = "Directory Created"
            Else
                Label1.Text = "Directory Exist"
            End If
        Catch _err As IOException
            Response.Write(_err.Message)
        End Try
    End
Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim NewDirectory As String = Server.MapPath("Abhimanyu Kumar Vatsa")
        'New Directory Name in string variable
        CreateDirectoryIfNotExists(NewDirectory)
        'Calling the function to create new directory
    End
Sub
End Class

That's all about the coding part. After placing above coding you just have to click on the button and it will do all for you.

HAVE A GREAT CODING!!


Similar Articles