In this article I will explain with an example, how to create Folder (Directory) and Upload file in ASP.Net using C# and VB.Net.
The Folder (Directory) will be created on the Server’s disk.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net FileUpload control, a Button and a Label control. The Button has been assigned a Click event handler.
<asp:FileUpload ID="FileUpload1" runat="server" />
<hr />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="UploadFile" />
<br />
<asp:Label ID="lblMessage" ForeColor="Green" runat="server" />
 
 
Namespaces
You will need to import the following namespace.
C#
using System.IO;
 
VB.Net
Imports System.IO
 
 
Create Folder (Directory) and Upload file in ASP.Net
When the Upload Button is clicked, first a check is performed whether the Folder (Directory) exists. If it does not then the Folder (Directory) is created.
Then the uploaded File is saved into the Folder (Directory).
Finally a success message is displayed on the screen using the Label control.
C#
protected void UploadFile(object sender, EventArgs e)
{
    string folderPath = Server.MapPath("~/Files/");
       
    //Check whether Directory (Folder) exists.
    if (!Directory.Exists(folderPath))
    {
        //If Directory (Folder) does not exists. Create it.
        Directory.CreateDirectory(folderPath);
    }
 
    //Save the File to the Directory (Folder).
    FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));
 
    //Display the success message.
    lblMessage.Text = Path.GetFileName(FileUpload1.FileName) + " has been uploaded.";
}
 
VB.Net
Protected Sub UploadFile(sender As Object, e As EventArgs)
    Dim folderPath As String = Server.MapPath("~/Files/")
 
    'Check whether Directory (Folder) exists.
    If Not Directory.Exists(folderPath) Then
        'If Directory (Folder) does not exists. Create it.
        Directory.CreateDirectory(folderPath)
    End If
 
    'Save the File to the Directory (Folder).
    FileUpload1.SaveAs(folderPath & Path.GetFileName(FileUpload1.FileName))
 
    'Display the success message.
    lblMessage.Text = Path.GetFileName(FileUpload1.FileName) + " has been uploaded."
End Sub
 
 
Screenshot
Create Folder (Directory) and Upload file in ASP.Net using C# and VB.Net
 
 
Downloads