IT Community - Software Programming, Web Development and Technical Support

How to Read and write txt file using C#

This is a discussion on How to Read and write txt file using C# within the C# Programming forums, part of the Software Development category; How to Read and write txt file using C#...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-24-2007, 05:18 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Question How to Read and write txt file using C#

How to Read and write txt file using C#
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-24-2007, 05:59 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: How to Read and write txt file using C#

Reading and Writing Text file:


Reading:

using System;
using System.IO;

namespace csharp_station.howto
{
class TextFileReader
{
static void Main(string[] args)
{
// create reader & open file
Textreader tr = new StreamReader("date.txt");

// read a line of text
Console.WriteLine(tr.ReadLine());

// close the stream
tr.Close();
}
}
}



Writing:


using System;
using System.IO;

namespace csharp_station.howto
{
class TextFileWriter
{
static void Main(string[] args)
{
// create a writer and open the file
TextWriter tw = new StreamWriter("date.txt");

// write a line of text to the file
tw.WriteLine(DateTime.Now);

// close the stream
tw.Close();
}
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-25-2007, 02:40 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: How to Read and write txt file using C#

We can read the file using File class as follows,

Code:
//Read a txt file

string[] str = File.ReadAllLines(@"C:/test.txt");
if (str.Length > 0)
 {
   for (int i = 0; i < str.Length; i++)
   {
     MessageBox.Show(str[i]);
   }
 }

We can write the file using StreamWriter as follows,

Code:
//Write a txt file

StreamWriter sw = new StreamWriter("C:/test.txt");
string str = "Test for writing";
sw.WriteLine(str);
sw.Close();
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Attempted to read write protected memory Balasubramanian.S C# Programming 0 03-21-2008 09:47 PM
How to read the text from a .pdf file? vadivelanvaidyanathan Software Testing 0 01-28-2008 10:15 PM
How to read and write System Registry Keys using C# (VS.Net 2005) oxygen C# Programming 2 08-31-2007 11:41 PM
Read and Write IPTC and EXIF metadata fro raw Images Balasubramanian.S C# Programming 4 08-03-2007 07:38 AM
Method in read the file prasath Java Programming 1 07-17-2007 12:12 AM


All times are GMT -7. The time now is 10:53 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0