You are on page 1of 17

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

NET
By: Suresh Dasari Apr 9, 2013 Categories: Asp.net, C#.Net, VB.NET

Introduction: Here I will explain how to create, delete directory or folder in asp.net using C# and VB.NET or Delete all Files in a Directory. Description: In previous articles I explained Delete files from uploaded folder in asp.net, Check username availability with progressbar, Richtextbox example in asp.net, Asp.net Interview questions, Joins in SQL Server, Highlight Gridview records based on search and many articles relating to Gridview, SQL ,jQuery,asp.net, C#,VB.NET. Now I will explain how to create, delete directory or folder in asp.net using C# and VB.NET. 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" FontBold="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" FontBold="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,

C# - Delete all Directories and SubDirectories in C#, VB.NET


By: Suresh Dasari Apr 10, 2013 Categories: Asp.net, C#.Net, Code Snippets, VB.NET

Introduction: Here I will explain how to delete all directories and subdirectories in asp.net using C# and VB.NET. Description: In previous articles I explained Create or Delete directory/folder in c#, Delete files from uploaded folder in asp.net, Check username availability with progressbar, Asp.net Interview questions, Joins in SQL Server and many articles relating to Gridview, SQL ,jQuery,asp.net, C#,VB.NET. Now I will explain how to delete all directories and subdirectories in asp.net using C# and VB.NET. To delete all directories and subdirectories first we need to add namespace using
System.IO Once we add namespace need to write the code like as shown below

C# Code

// Delete direcoty or folder protected void btnDelete_Click(object sender, EventArgs e) { string strpath = @"D:\" + txtdltName.Text; if (Directory.Exists(strpath))

{ RemoveDirectories(strpath); } } 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); } VB.NET Code

' 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) 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 If you want to see complete example check below url

http://www.aspdotnet-suresh.com/2013/04/aspnet-create-deletedirectoryfolder.html

If you enjoyed this post

C# - Delete Files in Folders and SubFolders in C#, VB.NET


By: Suresh Dasari Apr 10, 2013 Categories: Asp.net, C#.Net, Code Snippets, VB.NET

Introduction: Here I will explain how to delete files in folders and subfolders in asp.net using C# and VB.NET. Description: In previous articles I explained Create or Delete directory/folder in c#, Delete files from uploaded folder in asp.net, jQuery Dropdown Menu example, start and stop timer example in JavaScript, Asp.net Interview questions and many articles relating to Gridview, SQL, jQuery,asp.net, C#,VB.NET. Now I will explain how to delete files in folders and subfolders in asp.net using C# and VB.NET. To delete files in folders and subfolders first we need to add namespace using System.IO Once we add namespace need to write the code like as shown below C# Code

// Delete direcoty or folder protected void btnDelete_Click(object sender, EventArgs e) { string strpath = @"D:\" + txtdltName.Text; if (Directory.Exists(strpath)) { RemoveDirectories(strpath); } } 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); } VB.NET Code

' 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) 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 If you want to see complete example check below url

http://www.aspdotnet-suresh.com/2013/04/aspnet-create-delete-directoryfolder.html

If you enjoyed

8 Best jQuery Clock Plugins (Analog & Digital) Examples


By: Suresh Dasari Apr 10, 2013 Categories: JQuery, JQuery Plugins

Introduction: Here I will explain best jQuery clock plugins (analog & digital) examples. The jQuery clock plugins (analog & digital) is used to make your site more beautiful and attractive. Description: In previous article I explained 6 jQuery News ticker plugin examples, 4 SpellChecker Plugin examples, 4 jQuery Price Range slider examples, 11+ Best jQuery Countdown timer plugins, 12+ Best jQuery Drag and Drop Plugins, 11+ best jQuery Scroll to top plugins and many articles relating to jQuery Plugins, JQuery, Ajax, asp.net, SQL Server etc. Now I will explain best jQuery clock plugins (analog & digital) examples.

jQuery jDigiClock Digital Clock Plugin

jDigiClock is a jQuery plugin inspired from HTC Hero Clock Widget.

Source Demo

jQuery CoolClock - The Javascript Analog Clock


CoolClock is a customisable javascript analog clock.

Source Demo

jQuery colorful clock with css Plugin


This colorful jQuery & CSS clock, which will help you keep track of those precious last seconds of the year.

Source Demo

jQuery Gas Pump Clock plugin

Source Demo

CSS3 Digital Clock with jQuery

Source Demo

Old School Clock with CSS3 and jQuery

Source Demo

An analogue clock using only CSS


A working clock that optionally resorts to JavaScript to grab the current time

Source Demo

jQuery Sliding Clock


This jQuery clock will moves the slides along the scales based on the current date and time.

Source Demo

If you enjoyed this post

SharePoint 2010 - Sort Change the Order of Items in Quick Launch


By: Suresh Dasari Apr 10, 2013 Categories: SharePoint

Introduction: Here I will explain how to sort or change the Order of Items on the Quick launch of SharePoint 2010. In SharePoint 2010 we have Quick Launch tab in left side to display site pages which contains links, lists and libraries on the site. Description: In previous post I explained Add, Delete Quick Launch links in SharePoint 2010, Enable treeview in SharePoint 2010, Delete site from SharePoint site collection, change site theme in SharePoint 2010, Allow Anonymous access to SharePoint site, create new survey in

SharePoint 2010 and Delete users from group in SharePoint 2010. Now I will explain how to sort or change the Order of Items on the Quick launch of SharePoint 2010. To sort or change links order in quick launch of SharePoint 2010 we need to follow below steps 1. Once you open your site Go to Site Actions - Site Settings like as shown below figure

2. Once you open Site Settings in that go to Look and Feel and select Quick launch like as shown below

3. Once you open Quick launch we will have an option New Heading click on it to add new list or you can edit the existing links Libraries, Lists and Discussions by clicking on particular link in quick launch window like as shown below image

4. Once you click on Change Order Re-Order Links screen will appear like as shown below

5. Once you change the order of lists click OK and check your Quick Launch tab. All the items will reorder as per your orders.

If

You might also like