How to create SubDirectory


In my previous articles I try to explain how to create Directories and how to get files information etc.. Now, I'm trying to explain how to create Sub Directory under Directory. During this we learn how to create a sub directory in our system, this will help you for beginners those who are new to this.

How to create SubDirectory :



In my previous articles I try to explain how to create Directories and how to get files information etc.. Now, I'm trying to explain how to create Sub Directory under Directory. During this we learn how to create a sub directory in our system, this will help you for beginners those who are new to this.

Follow below steps to achieve your goal.

Step-1:



Create a project and then right click on solution explorer then choose NewItem and then choose webform then give a name for that as "CreateSubDir.aspx".

Step-2:



After create a page simply drag and drop 3 label controls, 2 textbox controls and 1 button control into the design view.

Step-3:



After drag and drop all the controls rearrange them in proper order and give a proper names to that. After redesign the page the page becomes like below.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateSubDir.aspx.cs" Inherits="ASPnet_CreateSubDir" %>

<!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>

<asp:Label ID="lblRootDir" runat="server"
Text="Enter path and name of source directory :"></asp:Label>

<asp:TextBox ID="txtRootDir" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Enter name of Sub directory :"></asp:Label>

<asp:TextBox ID="txtSubDir" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnSubDir" runat="server" onclick="btnSubDir_Click"
Text="Create SubDirectory" />
<br />
<br />
<asp:Label ID="lblResult" runat="server"></asp:Label>

</div>
</form>
</body>
</html>


Step-4:



After design a page then double click on the button control then on click event of button wrote below lines of code.

protected void btnSubDir_Click(object sender, EventArgs e)
{
string DirPath = txtRootDir.Text;

System.IO.DirectoryInfo obj = new System.IO.DirectoryInfo(DirPath);
try
{
string SubDirName = txtSubDir.Text;
obj.CreateSubdirectory(SubDirName);
lblResult.Text = "SubDirectory : " + SubDirName + " is created under - " + DirPath;
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
}


Step-5:



Once everything is completed successfully then execute the program by clicking on f5 on keyboard then check the output. And then check the appropriate folder that sub directory has been created or not.

1

Source Code:




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ASPnet_CreateSubDir : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubDir_Click(object sender, EventArgs e)
{
string DirPath = txtRootDir.Text;

System.IO.DirectoryInfo obj = new System.IO.DirectoryInfo(DirPath);
try
{
string SubDirName = txtSubDir.Text;
obj.CreateSubdirectory(SubDirName);
lblResult.Text = "SubDirectory : " + SubDirName + " is created under - " + DirPath;
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
}
}


Conclusion:



Hope this article will help you those who are looking for the same and those who are new to this concept and this article mostly useful for fresher's.


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: