Creating and deleting a directory is very important for a community website where a user posts articles, forum messages etc. When the user registers for a website then that folder can be created dynamically and after that the data can be automatically placed inside their own directory instead of the root or anywhere else. Creating a 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>
<b>Directory will be created at root of project.</b><br /><br />
</div>
<div>
<br />
<asp:Button ID="Button1" runat="server" Text="Create New Directory"
onclick="Button1_Click" />
<br /><br />
<asp:Button ID="Button2" runat="server" Text="Delete Directory"
onclick="Button2_Click" />
<br /><br />
<asp:Label ID="Label1" runat="server" Text="" ForeColor="Red"></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)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string NewDirectory = Server.MapPath("Abhimanyu Kumar Vatsa");
//New Directory Name in string variable
CreateDirectoryIfNotExist(NewDirectory);
//Calling the function to create new directory
}
protected void Button2_Click(object sender, EventArgs e)
{
string NewDirectory = Server.MapPath("Abhimanyu Kumar Vatsa");
//New Directory Name in string variable
DeleteDirectoryIfExist(NewDirectory);
//Calling the function to create new directory
}
private void CreateDirectoryIfNotExist(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);
}
}
private void DeleteDirectoryIfExist(string NewDirectory)
{
try
{
// Checking the existance of directory
if (Directory.Exists(NewDirectory))
{
//If No any such directory then creates the new one
Directory.Delete(NewDirectory);
Label1.Text = "Directory Deleted";
}
else
{
Label1.Text = "Directory Not Exist";
}
}
catch (IOException _err)
{
Response.Write(_err.Message);
}
}
}
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
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
CreateDirectoryIfNotExist(NewDirectory)
'Calling the function to create new directory
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim NewDirectory As String = Server.MapPath("Abhimanyu Kumar Vatsa")
'New Directory Name in string variable
DeleteDirectoryIfExist(NewDirectory)
'Calling the function to create new directory
End Sub
Private Sub CreateDirectoryIfNotExist(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
Private Sub DeleteDirectoryIfExist(ByVal NewDirectory As String)
Try
' Checking the existance of directory
If Directory.Exists(NewDirectory) Then
'If No any such directory then creates the new one
Directory.Delete(NewDirectory)
Label1.Text = "Directory Deleted"
Else
Label1.Text = "Directory Not Exist"
End If
Catch _err As IOException
Response.Write(_err.Message)
End Try
End Sub
End Class
That's all about the coding part. After placing the above code you just have to click on the button and it will do it all for you.
HAVE A GREAT CODING!!