Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Tutorials > CSharp Tutorials

Vote on your favorite hash algorithm here!

CSharp Tutorials Tutorials for C#

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-02-2008, 01:22 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,707
Last Blog:
Web slideshow in JavaS...
Credits: 1,455
Rep Power: 62
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
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!
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Mr. Xav | Website | Forums | Blog
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
Posts: 7,508
Last Blog:
Tramp Variables
Credits: 1
Rep Power: 20
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 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
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!
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
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,707
Last Blog:
Web slideshow in JavaS...
Credits: 1,455
Rep Power: 62
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
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!
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Mr. Xav | Website | Forums | Blog
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
Posts: 7,508
Last Blog:
Tramp Variables
Credits: 1
Rep Power: 20
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 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
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!
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
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,707
Last Blog:
Web slideshow in JavaS...
Credits: 1,455
Rep Power: 62
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
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!
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-28-2008, 06:50 AM
Keuvie Keuvie is offline
Newbie
 
Join Date: Jul 2008
Posts: 13
Credits: 0
Rep Power: 0
Keuvie is on a distinguished road
Default Re: C# Tutorial: Writing Text Files

ok, i got 2 questions.

the first one is for vista. The program i only allowed to write to a file if i manually select "Run as administrator". How to solve this?

The decond question. How to ADD lines to a text file?

Keuvie
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-28-2008, 09:37 AM
Core2quad Core2quad is offline
Newbie
 
Join Date: Jul 2008
Posts: 3
Credits: 0
Rep Power: 0
Core2quad is on a distinguished road
Default Re: C# Tutorial: Writing Text Files

Xav and jordan, I am new here but wanted to learn lots new things and show lots of new things to Users here...thanks for the wonderful posts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-28-2008, 12:42 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,707
Last Blog:
Web slideshow in JavaS...
Credits: 1,455
Rep Power: 62
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default Re: C# Tutorial: Writing Text Files

Quote:
Originally Posted by Keuvie View Post
the first one is for vista. The program i only allowed to write to a file if i manually select "Run as administrator". How to solve this?
Hmm, I'm not sure about that one. I suppose it's some sort of safety measure, but I don't know how to correct it. Perhaps Google will have the answer?

Quote:
Originally Posted by Keuvie View Post
The second question. How to ADD lines to a text file?
Do you want to add text to the middle/beginning of the file or append text onto the end?

Quote:
Originally Posted by Core2quad View Post
Xav and jordan, I am new here but wanted to learn lots new things and show lots of new things to Users here...thanks for the wonderful posts.
You're welcome! Why not post an Introduction thread in the Introductions forum to really begin as a member of CodeCall?
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-28-2008, 01:03 PM
Keuvie Keuvie is offline
Newbie
 
Join Date: Jul 2008
Posts: 13
Credits: 0
Rep Power: 0
Keuvie is on a distinguished road
Talking Re: C# Tutorial: Writing Text Files

Quote:
Originally Posted by Xav View Post
...
Do you want to add text to the middle/beginning of the file or append text onto the end?

...
the first thing i want to learn is to add text at the end
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 07-28-2008, 01:24 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,707
Last Blog:
Web slideshow in JavaS...
Credits: 1,455
Rep Power: 62
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default Re: C# Tutorial: Writing Text Files

You only need to change one line:

Replace this:
csharp Code:
  1. StreamWriter sw = new StreamWriter(path)
... with this:
csharp Code:
  1. StreamWriter sw = new StreamWriter(path, True)
The extra parameter at the end means "do you want to append text on to the end of the file?" Specifying False, or just leaving it, overwrites the file.
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Mr. Xav | Website | Forums | Blog
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 13 09-23-2008 06:11 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 3 09-15-2007 10:08 PM
HTML Basic Formatting clookid Tutorials 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 09:09 PM.