How to Create a File in Directory


In my previous post i try to explain how to create a directory and how to get directory files and display into our system. Now, i try to explain how to create a file in a directory using C#.net, this article will help you for beginners and those who are beginners to perform this task.

Creating a File:



In my previous post i try to explain how to create a directory and how to get directory files and display into our system. Now, i try to explain how to create a file in a directory using C#.net, this article will help you for beginners and those who are beginners to perform this task.

Follow below steps to achieve your goal.

Step-1:


Create a project solution, then right click on solution explorer then choose Add->NewItem, then choose webform and give a name for that as "CreateFile.aspx".

Step-2:


After create a page then open that page in design view then design page like below. See the below source code for that.

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

<!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="lblPath" runat="server" Text="Enter Name and path of file :"></asp:Label>
<asp:TextBox ID="txtPath" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblText" runat="server" Text="Write Text :"></asp:Label>

<br />
<br />
<asp:TextBox ID="txtText" runat="server" Height="210px" TextMode="MultiLine"
Width="577px"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblResult" runat="server"></asp:Label>
<br />
<br />
<asp:Button ID="btnGenerate" runat="server" onclick="btnGenerate_Click"
Text="Generate File" />


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


Step-3:


After design the page the page becomes like below.

1

Step-4:


Once design and everything is completed then double click on Generate File button and wrote below lines of code on click event of that button.

protected void btnGenerate_Click(object sender, EventArgs e)
{
string Path;
System.IO.StreamWriter sw;

try
{
Path = txtPath.Text;
//check that file name is exists or not
if (!System.IO.File.Exists(Path))
{
//create text file whatever enter by user
sw = System.IO.File.CreateText(Path);
//write the text entered by user and store that text into that file.
sw.WriteLine(txtText.Text);
sw.Close();
lblResult.Text = "File Created";
}
else
{
lblResult.Text = "The file already exists with this location please change the file name";
}
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
}


Step-5:


Now, everything is completed successfully then execute that code by click on F5 on keyboard and then see the result set.

2

Step-6:



Now, the file is created successfully under Naveen folder in E drive, please check that and open that file and see the content.

Conclusion:



Hope this article will help you for beginners.


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

Author: adeyeye michael18 Feb 2014 Member Level: Bronze   Points : 0

This is a good resource that is really helpful to newbies. I will also practice this and be perfect in this.



  • 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: