Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Create Delete Directory Folder in Asp.net, C#, VB.NET

Apr 9, 2013
Introduction:

Here I will explain how to create directory or folder, delete directory or folder in asp.net using C# and VB.NET or Delete all Files in a Directory.

Description:


To create or delete directory/folder we need to write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Create or Delete Directory/floder in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td><b>Enter File Name to Create:</b></td>
<td><asp:TextBox ID="txtName" runat="server" /></td>
<td> <asp:Button ID="btnCreate" runat="server" Text="Create Directory" Font-Bold="true" onclick="btnCreate_Click" /> </td>
</tr>
<tr>
<td><b>Enter File Name to Delete:</b></td>
<td><asp:TextBox ID="txtdltName" runat="server" /></td>
<td> <asp:Button ID="btnDelete" runat="server" Text="Delete Directory" Font-Bold="true" onclick="btnDelete_Click" /> </td>
</tr>
<tr><td colspan="3"><asp:Label ID="lblResult" runat="server" ForeColor="Red" /> </td></tr>
</table>
</form>
</body>
</html>
Now in code behind add the following namespaces

C# Code


using System;
using System.IO;
Once you add namespaces write the following code in code behind


// Create new directory
protected void btnCreate_Click(object sender, EventArgs e)
{
string strpath = @"D:\" + txtName.Text;
//Condition to check if any directory exists with same name
if (!(Directory.Exists(strpath)))
{
Directory.CreateDirectory(strpath);
lblResult.Text = "Directory Created";
}
else
{
lblResult.Text = "Already Directory Exists with the same name";
}
}
// Delete direcoty or folder
protected void btnDelete_Click(object sender, EventArgs e)
{
string strpath = @"D:\" + txtdltName.Text;
if (Directory.Exists(strpath))
{
RemoveDirectories(strpath);
}
else
{
lblResult.Text = "Directory not exists";
}
}

private void RemoveDirectories(string strpath)
{
//This condition is used to delete all files from the Directory
foreach (string file in Directory.GetFiles(strpath))
{
File.Delete(file);
}
//This condition is used to check all child Directories and delete files
foreach (string subfolder in Directory.GetDirectories(strpath))
{
RemoveDirectories(subfolder);
}
Directory.Delete(strpath);
lblResult.Text = "Directory deleted";
}
VB.NET Code


Imports System.IO
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub
' Create new directory
Protected Sub btnCreate_Click(sender As Object, e As EventArgs)
Dim strpath As String = "D:\" + txtName.Text
'Condition to check if any directory exists with same name
If Not (Directory.Exists(strpath)) Then
Directory.CreateDirectory(strpath)
lblResult.Text = "Directory Created"
Else
lblResult.Text = "Already Directory Exists with the same name"
End If
End Sub
' Delete direcoty or folder
Protected Sub btnDelete_Click(sender As Object, e As EventArgs)
Dim strpath As String = "D:\" + txtdltName.Text
If Directory.Exists(strpath) Then
RemoveDirectories(strpath)
Else
lblResult.Text = "Directory not exists"
End If
End Sub

Private Sub RemoveDirectories(strpath As String)
'This condition is used to delete all files from the Directory
For Each file1 As String In Directory.GetFiles(strpath)
File.Delete(file1)
Next
'This condition is used to check all child Directories and delete files
For Each subfolder As String In Directory.GetDirectories(strpath)
RemoveDirectories(subfolder)
Next
Directory.Delete(strpath)
lblResult.Text = "Directory deleted"
End Sub
End Class
Demo



If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

4 comments :

tinhdamsugia said...

If you demo with 3 tiers is very good, because all application often use 3 tiers.
If you can, you are to do so.

tinhdamsugia said...

I'd like your examples.

Anonymous said...

r u big appateker (tinhdamsugia)

sudhir said...

Not able create folder at live site. Please help me.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.