Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old July 6th, 2007, 10:09 PM
Atran's Avatar
Needs Regular Fix
 
Join Date: May 2007
Location: Sweden
Age: 15
Posts: 323
Default Write/Read A Text File - C#

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.
Reply
  #2  
Old February 13th, 2008, 03:47 PM
Rii Rii is offline
Newbie
 
Join Date: Feb 2008
Posts: 2
Default

I think it 'll hlp me in project...
Thanks
Reply
  #3  
Old February 26th, 2008, 07:11 AM
Newbie
 
Join Date: Feb 2008
Posts: 3
Default

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...
Reply
  #4  
Old March 5th, 2008, 01:04 PM
Newbie
 
Join Date: Mar 2008
Posts: 1
Default

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.  
Reply
  #5  
Old July 10th, 2008, 10:49 AM
Newbie
 
Join Date: Jul 2008
Posts: 3
Default

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?
Reply
  #6  
Old September 9th, 2008, 06:39 AM
Newbie
 
Join Date: Sep 2008
Posts: 2
Default

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
Reply
  #7  
Old September 9th, 2008, 06:43 AM
Newbie
 
Join Date: Sep 2008
Posts: 2
Default

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

plz reply as soon as possible
Reply
  #8  
Old November 6th, 2008, 10:26 AM
Newbie
 
Join Date: Nov 2008
Posts: 1
Default

Quote:
Originally Posted by misbahiqbal
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!
Reply
Reply

Bookmarks

Thread Tools

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

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

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles