asp.net - How to create a directory

Create a directory programmatically
.Net framework Directory.CreateDirectory(String) method allow us to create all directories and sub directories in the specified path unless they already exists. Directory class CreateDirectory() method exists in System.IO namespace.

CreateDirectory(String) method requires to pass a parameter named 'path'. this 'path' parameter value data type is System.String which represents the directory to create.

CreateDirectory(String) method return value type is System.IO.DirectoryInfo that represents an object which represents the directory at the specified path. this object is returned regardless of whether a directory at the specified path already exists.

CreateDirectory(String) method throw IOException, if the directory specified by 'path' is a file or the network name is not known. it throw UnauthorizedAccessException, if the caller does not have the required permission.

method throw ArgumentNullException, if 'path' is null. it throw DirectoryNotFoundException, if the specified path is invalid. method also throw ArgumentException, PathTooLongException and NotSupportedException.

Directory.CreateDirectory(String, DirectorySecurity) overloaded method allow us to create all the directories in the specified path, unless they already exists and it also apply the specified windows security.

.Net framework DirectoryInfo class DirectoryInfo.Create() method also allow us to create a directory. in the bellow example code we applied DirectoryInfo.Create() method to create a new directory.

the following asp.net c# example code demonstrate us how can we create a directory programmatically at run time in an asp.net application.
CreateDirectory.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {

        Label1.Text = "Physical Application Path: " + Request.PhysicalApplicationPath;
        Label1.Text += "<br/ >Create a directory here...";
    }

    protected void Button1_Click(object sender, System.EventArgs e) {
        string appPath = Request.PhysicalApplicationPath.ToString();
                
        DirectoryInfo newDirectory = new DirectoryInfo(appPath+TextBox1.Text);
        try 
        {
            newDirectory.Create();
            Label2.Text = "Directory created successfully!";
        }
        catch(Exception ex)
        {
        Label2.Text = "an error occured when create the directory!";
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to create a directory programmatically in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">asp.net example: create directory</h2>
        <asp:Label 
            ID="Label1" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="SeaGreen"
            >
        </asp:Label>
        <br />
        <asp:Label 
            ID="Label2" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="Crimson"
            Font-Italic="true"
            >
        </asp:Label>
        <br /><br />
        <asp:Label 
            ID="Label3" 
            runat="server" 
            Text="Directory Name"
            >
        </asp:Label>
        <asp:TextBox ID="TextBox1" runat="server">
        </asp:TextBox>
        <br /><br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Font-Bold="true" 
            ForeColor="HotPink"
            Text="Create Directory"
            OnClick="Button1_Click"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples