ARTICLE

How to create a folder and directory in ASP.NET using VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET April 15, 2011
This Article defines how to create folder and directory in ASP.NET.
 
Reader Level:

This Article defines how to create folder and directory in ASP.NET.

Creating Folder in ASP.NET

In the designing form we take a Button control and a Label control and Button named create Folder. The below example defines that in simplest way.

The designing form looks like this.

d1.gif

Figure1

aspx code

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication37.WebForm1" %>

 

<!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 id="Head1" runat="server">

    <title></title>

</head>

<body style="height: 5px">

    <form id="form1" runat="server">

    <p>

        <asp:Button ID="Button2" runat="server" onclick="Button2_Click"

            style="top: 56px; left: 204px; position: absolute; height: 34px; width: 142px"

            Text="Create Directory"/>

   </p>

    <p>

        <asp:Label ID="Label2" runat="server"

            style="top: 99px; left: 244px; position: absolute; height: 12px; width: 34px"

            Text="Label"></asp:Label>

        <asp:Button ID="Button3" runat="server" style="margin-bottom: 0px"

        Text="Create Folder" onclick="Button3_Click1" />

    </p>

    <p>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>

    </p>

    </form>

</body>

</html>

VB code

Protected Sub Button3_Click1(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click

        Dim pathToCreate As String = "D:\Rohatash2"

        System.IO.Directory.CreateDirectory(pathToCreate)

        Label3.Text = "Folder created"

Now creating directory in server

To create directory in server we use server.Mappath class.

VB code

Private Sub CreateDirectoryIfNotExists(ByVal NewDirectory As String)

        Try

            If Not System.IO.Directory.Exists(NewDirectory) Then

                System.IO.Directory.CreateDirectory(NewDirectory)

                Label2.Text = "Directory Created"

            Else

                Label2.Text = "Directory Exist"

            End If

        Catch _err As IOException

            Response.Write(_err.Message)

        End Try

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click

        Dim NewDirectory As String = Server.MapPath("Rohatash Kumar")

        CreateDirectoryIfNotExists(NewDirectory)

    End Sub

Now run the application and test it.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor