Create a Directory in our System
In my previous article I'm trying to explain how to show directory information in to our application. Now, I'm trying to explain how to create a new Directory in to our system and how to show the message if directory has been already exists. Hope this article will help you those who are new to perform this.
Create a Directory:
In my previous article I'm trying to explain how to show directory information in to our application. Now, I'm trying to explain how to create a new Directory in to our system and how to show the message if directory has been already exists. Hope this article will help you those who are new to perform this.
Follow below steps to achieve your goal.Step-1:
Create a project and then right click on Solution explorer then choose AddNewItem for creating new web page and give a name for that as "CreateDirectory.cs".Step-2:
Once create a web page then design page, simply drag and drop 2 label controls, 1 text box control and one button control in to the design view. And give proper Names and Id's for them.Step-3:
After Design the page the view looks becomes like below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateDirectory.aspx.cs" Inherits="ASPnet_CreateDirectory" %>
<!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="lblDir" runat="server" Text="Enter Directory Path :"></asp:Label>
<asp:TextBox ID="txtDir" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnCreate" runat="server" onclick="btnCreate_Click"
Text="Create Directory" />
<br />
<br />
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</div>
</form>
</body>
</html>Step-4:
Once design has been completed then double click on the button control then wrote code inside button click event like below.
protected void btnCreate_Click(object sender, EventArgs e)
{
string Path = txtDir.Text;
try
{
if (!System.IO.Directory.Exists(Path))
{
System.IO.Directory.CreateDirectory(Path);
lblStatus.Text = " Directory is created for the specified path.. " + Path;
}
else
{
lblStatus.Text = "Directory is already created at specified path.." + Path;
}
}
catch (Exception ex)
{
lblStatus.Text = "Please check the Path of the Directory you entered.." + ex.Message;
}
}
In above mentioned code CreateDirectory is used to create New Directory in our system.Step-5:
Once code part is executed then check the output by click on F5. 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_CreateDirectory : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCreate_Click(object sender, EventArgs e)
{
string Path = txtDir.Text;
try
{
if (!System.IO.Directory.Exists(Path))
{
System.IO.Directory.CreateDirectory(Path);
lblStatus.Text = " Directory is created for the specified path.. " + Path;
}
else
{
lblStatus.Text = "Directory is already created at specified path.." + Path;
}
}
catch (Exception ex)
{
lblStatus.Text = "Please check the Path of the Directory you entered.." + ex.Message;
}
}
}Conclusion:
Hope this article will help you those who are new to this and those who are beginners to perform this type of operations.