In this article I will explain with an example, how to create Folder (Directory) and upload file in ASP.Net 3.5 using C# and VB.Net.
First, a Folder (Directory) will be created if not exists and then, the file will be uploaded and saved using C# and VB.Net.
This article makes use of ASP.Net version 3.5.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net FileUpload control, a Button and a Label control.
The Button has been assigned with an OnClick 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
 
 
Creating Folder (Directory) and Uploading file in ASP.Net 3.5
When the Upload Button is clicked, following event handler is executed.
Inside this event handler, 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 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
ASP.Net 3.5: Create Folder (Directory) and Upload file using C# and VB.Net
 
 
Downloads


Other available versions