Lost Password?

  #1 (permalink)  
Old 05-02-2008, 01:22 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 3,021
Last Blog:
I thought about progra...
Rep Power: 26
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Post C# Tutorial: Writing Text Files

Writing Text Files in C# .NET
By Xav

Hello, Xav here. There are many amazing things you can do with an application, but one of the most useful is to interact with the user's files. Most programs store data in files, and this can be used for numerous reasons.

Here we are using the .NET Framework (I use v3.5). First, add the following statement to the top of the code file, next to the other "using" statements:

Code:
using System.IO;
The .NET Framework provides a namespace for working with the data - System.IO (short for Input/Output). Now - on to reading the most basic of files - the text file!

Writing Data

It is easier to write data to a file. Here's what you do:

We use a StreamWriter class (in the System.IO namespace) to write the data:
Code:
string path = @"C:\myfile.txt";
StreamWriter sw = new StreamWriter(path)
The object is not static - you create a new object (which we here call "sw"), and use the constructor to choose a file. Replace the string "path" with your desired path - and don't forget to include the @ symbol, otherwise C# interprets the backslashes as escape sequences. Next, is the easy part:
Code:
string bufferOne = "This is text 1.";
string bufferTwo = "This is text 2.";
sw.Write(bufferOne);
sw.Write(bufferTwo);
This writes the following text to the text file:

Quote:
This is text 1.This is text 2.
If we wanted to have the text on separate lines, we could instead use:
Code:
sw.WriteLine(bufferOne);
sw.WriteLine(bufferTwo);
This produces the following result in the text file:

Quote:
This is text 1.
This is text 2.
It depends on the situation as to which method to use. However, you must use whichever one is most appropriate for the situation.

At the end, we must finish off by closing and unreferencing the StreamWriter object. If you do not call Close(), the code may not work:
Code:
sw.Close();
sw.Dispose();
And that's all there is to it! Remember that you do not have to pass a string in to the Write() or WriteLine() - the method is overloaded, so you can use many data types, including char, byte, int and even boolean. Happy writing!
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-07-2008, 11:43 AM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 25
Posts: 4,536
Last Blog:
PHP: list()
Rep Power: 50
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: C# Tutorial: Writing Text Files

Nice tutorial Xav! +rep
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-07-2008, 02:03 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 3,021
Last Blog:
I thought about progra...
Rep Power: 26
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: C# Tutorial: Writing Text Files

Thanks! I'm really starting to enjoy this tutorial business - I must post a few more...
Well, it brings in the rep, so I'm happy - the best bit is, I have 3 bars, like TcM!
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-07-2008, 05:59 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 25
Posts: 4,536
Last Blog:
PHP: list()
Rep Power: 50
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: C# Tutorial: Writing Text Files

If you post enough tutorials like this you'll soon have more than him!
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-08-2008, 02:32 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 3,021
Last Blog:
I thought about progra...
Rep Power: 26
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: C# Tutorial: Writing Text Files

He won't really like that, will he?

Hang on a minute... GREAT!
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply

Tags
file, text, tutorial, writing



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB 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
Tutorial: Storing Images in MySQL with PHP Jordan PHP Tutorials 10 06-22-2008 07:02 PM
Text files travy92 Visual Basic Programming 1 10-07-2007 10:06 AM
How to style fonts of a text in a simple page? c0de Tutorials, Classes and Code 3 09-15-2007 10:08 PM
HTML Basic Formatting clookid Tutorials, Classes and Code 14 03-06-2007 03:10 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 03:05 PM


All times are GMT -5. The time now is 10:36 AM.

Contest Stats

dargueta ........ 93.00000
John ........ 87.50000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads