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.
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".
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">
<form id="form1" runat="server">
<asp:Label ID="lblPath" runat="server" Text="Enter Name and path of file :"></asp:Label>
<asp:TextBox ID="txtPath" runat="server"></asp:TextBox>
<asp:Label ID="lblText" runat="server" Text="Write Text :"></asp:Label>
<asp:TextBox ID="txtText" runat="server" Height="210px" TextMode="MultiLine"
<asp:Label ID="lblResult" runat="server"></asp:Label>
<asp:Button ID="btnGenerate" runat="server" onclick="btnGenerate_Click"
Text="Generate File" />
After design the page the page becomes like below.
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)
Path = txtPath.Text;
//check that file name is exists or not
//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.
lblResult.Text = "File Created";
lblResult.Text = "The file already exists with this location please change the file name";
catch (Exception ex)
lblResult.Text = ex.Message;
Now, everything is completed successfully then execute that code by click on F5 on keyboard and then see the result set.
Now, the file is created successfully under Naveen folder in E drive, please check that and open that file and see the content.
Hope this article will help you for beginners.
This is a good resource that is really helpful to newbies. I will also practice this and be perfect in this.