ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

C#: Create and Read Text File

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 – jannopalacios@gmail.com
Mobile No. – 09305235027 – tnt

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!