Create and Read Text File in C#

Create and Read Text File in C#

csharpReadCreatefig.3Today, you will learn how to create and read Text file using C#.net. This method is very important because it represents as a log file that you will need in a system. Logs are use to track all events in the system.

Let’s begin:

1. Open Microsoft Visual Studio 2008 and create new Windows Form Application for C#. Then do the following design of a Form just like this.

csharpReadCreatefig.1

2. Go to the Solution Explorer, click the “View Code”  to display the code editor.

csharpReadCreatefig.2

3. Set the path where you want to place a text file.

 string path = @"c:/Test.txt";

4. Go back to the design view, double click the “Create” button and do the following codes for creating a text file.

 //checking if the file exist
 if (!File.Exists(path))
 {
 // Create a txt file to write the text in the textbox.
 using (StreamWriter sw = File.CreateText(path))
 {
 sw.WriteLine(txtWriteLog.Text); 
 }
 }
 //checking if the file exist
 if (File.Exists(path))
 {
 // This text in the textbox will always appen into the .txt file that you have created.
 using (StreamWriter sw = File.AppendText(path))
 {
 sw.WriteLine(txtWriteLog.Text);
 }
 }

5. Go back to the design view, double click the “View Logs” button and do the following codes to open a text file that you have created.

 //checking if file exist
 if (File.Exists(path))
 { 
 //open the notepad(file) that you have created
 System.Diagnostics.Process.Start(@"notepad.exe", path); 
 }

For all students who need programmer for your thesis system or anyone who needs a sourcecode in any programming languages. You can contact me @ :

Email – [email protected]
Mobile No. – 09305235027 – tnt

ABOUT PROJECTPROJECT DETAILS
Project Name :Create and Read Text File
Project Platform :C#
Programming Language Used:C# Programming Language
Developer Name :itsourcecode.com
IDE Tool (Recommended):Visual Studio 2019
Project Type :Desktop Application
Database:None
Upload Date and Time:June 23, 2016 – 6:33 am

Leave a Comment