By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,491 Members | 1,168 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Write/Read A Text File - C#

Atran
100+
P: 319
Hello:
In this article: You will learn to Write or Read A Text File.

Let's Begin:
First Create a new project (ConsoleApp or WinApp).
And Make sure your program uses these namespaces:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Diagnostics;
  4.  
Now, we will begin writing text to a file:
1)- Create a new stream-writer and open the file:
Expand|Select|Wrap|Line Numbers
  1. TextWriter tw = new StreamWriter(@"C:\Hello.txt");
  2.  
2)- Write text to the file:
Expand|Select|Wrap|Line Numbers
  1. tw.WriteLine("Hello");
  2.  
3)- Close the file.
Expand|Select|Wrap|Line Numbers
  1. tw.Close();
  2.  
4)- Launch the file:
Expand|Select|Wrap|Line Numbers
  1. Process.Start(@"C:\Hello.txt");
  2.  
Here is all the code:
Expand|Select|Wrap|Line Numbers
  1. //Creating a new stream-writer and opening the file.
  2. TextWriter tsw = new StreamWriter(@"C:\Hello.txt");
  3.  
  4. //Writing text to the file.
  5. tsw.WriteLine("Hello");
  6.  
  7. //Close the file.
  8. tsw.Close();
  9.  
  10. //Launch the file.
  11. Process.Start(@"C:\Hello.txt");
  12.  
  13. Console.WriteLine("You're done, press any key to exit...");
  14. Console.ReadKey();
  15.  

Now, we will begin reading text from a file:
1)- Creating a new stream-reader and opening the file:
Expand|Select|Wrap|Line Numbers
  1. TextReader trs = new StreamReader(@"C:\Hello.txt");
  2.  
2)- Reading text of the file:
Expand|Select|Wrap|Line Numbers
  1. //Reading all the text of the file.
  2. Console.WriteLine(trs.ReadToEnd());
  3.  
  4. //Or Can Reading a line of the text file.
  5. //Console.WriteLine(trs.ReadLine());
  6.  
3)- Close the file:
Expand|Select|Wrap|Line Numbers
  1. trs.Close();
  2.  
Here is all the code:
Expand|Select|Wrap|Line Numbers
  1. //Creating a new stream-reader and opening the file.
  2. TextReader trs = new StreamReader(@"C:\Hello.txt");
  3.  
  4. //Reading all the text of the file.
  5. Console.WriteLine(trs.ReadToEnd());
  6.  
  7. //Or Can Reading a line of the text file.
  8. //Console.WriteLine(trs.ReadLine());
  9.  
  10. //Close the file.
  11. trs.Close();
  12.  
  13. Console.WriteLine("Press any key to exit...");
  14. Console.ReadKey();
  15.  
Hope this helped you.
Jul 6 '07 #1
Share this Article
Share on Google+
6 Comments


P: 2
Rii
I think it 'll hlp me in project...
Thanks
Feb 13 '08 #2

P: 3
hi
i need like this hw can i read and write the data into and from .mdb files ie MS ACCESS files.... using C#.. i need that if i enterd a text in a textbox it wil go and store into a .mdb file columns.. and hw can i retrive it back...
Feb 26 '08 #3

P: 1
Expand|Select|Wrap|Line Numbers
  1. // remember that you name your class GetDataFromMDB and store it under this filename
  2. using System;
  3. using System.IO;
  4. using System.Data;
  5. using System.Data.OleDb;
  6.  
  7. namespace OLE_DataGet  // Use your project-namespace here
  8. {
  9.     public class GetDataFromMDB
  10.     {
  11.         private DataTable mDataTable = new DataTable();
  12.  
  13.         #region constructor 
  14.         /// <summary>
  15.         /// The constructor itself
  16.         /// </summary>
  17.         public GetDataFromMDB
  18.         {
  19.             // Anything you will do here, do it.
  20.         }
  21.         #endregion
  22.  
  23.  
  24.         #region MS Access (MDB) access
  25.         /// <summary>
  26.         /// Initialize and read a MDB (MS-Access-File)[here "MDB_FileName.MDB"] and 
  27.                 /// store 3 field [here TableItem1,TableItem2,TableItem3] out of the 
  28.                 /// database into a DataTable (mDataTable)
  29.         /// </summary>
  30.         /// <param name="pathToMDBfile">combined path and filename to the DataBaseFile</param>
  31.         /// <returns>true if success</returns>
  32.         private bool ReadDataBaseDBF(string pfad)
  33.         {
  34.             bool done = false;
  35.             string source = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + pathToMDBfile;
  36.             // select Table(MDB_FileName) und FieldNames(TableItem1,TableItem2,TableItem3)
  37.                         // to be read.
  38.                         // this is the SQL select message for the Jet-Engine to be used here
  39.             string select = "SELECT TableItem1, " + 
  40.                                                "TableItem2, " +
  41.                                "TableItem3 " +
  42.                            "FROM MDB_FileName";
  43.             try
  44.             {
  45.                 OleDbConnection conn = new OleDbConnection(source);
  46.                 conn.Open();
  47.                 OleDbCommand cmd = new OleDbCommand(select, conn);
  48.                 OleDbDataReader reader = cmd.ExecuteReader();
  49.                 // read TableItems to DataTable
  50.                 while(reader.Read())
  51.                 {
  52.                     this.mDataTable.Add(reader.GetString(0), 
  53.                                                             reader.GetString(1), 
  54.                                                             reader.GetString(2));
  55.                 }
  56.                 reader.Close();
  57.                 conn.Close();
  58.                 done = true;
  59.             }
  60.             catch (Exception e)
  61.             {
  62.                 // Whatever you will do with exception-handling, do it here
  63.             }
  64.             return done;
  65.         }
  66.         #endregion
  67.  
  68.                 #region properties
  69.  
  70.         /// <summary>
  71.         /// get your Datatable
  72.         /// </summary>                
  73.         /// <returns>the DataTable or null</returns>
  74.                 public DataTable MDB_DataTable
  75.                 {
  76.                    get
  77.                    {return mDataTable;}
  78.                 }
  79.                 #endregion
  80.     }
  81. }
  82.  
Mar 5 '08 #4

P: 3
How can i ensure that i have received a file completely from an external source?Suppose i am receiving a file from an external source and i try to read the file before i have recieved the entire file.How can i ensure that the file has been received completely?
Jul 10 '08 #5

P: 1
i need help regarding writting txt file data to access database because the file is of 200+MB size and the system is stuck when i open it.

plz help as soon as possible
Sep 9 '08 #6

P: 1
i need help regarding writing txt file data to access database. the file size is 200+MB

plz reply as soon as possible

Hello dude! Are you going to transfer all the data from your database to a text file?

All data i mean!
Nov 6 '08 #7