Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# Tutorial: Writing Text Files

streamwriter

  • Please log in to reply
37 replies to this topic

#1 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 02 May 2008 - 10:22 AM

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:

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:
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:
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:

This is text 1.This is text 2.


If we wanted to have the text on separate lines, we could instead use:
sw.WriteLine(bufferOne);
sw.WriteLine(bufferTwo);

This produces the following result in the text file:

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:
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!
  • 3
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 07 May 2008 - 08:43 AM

Nice tutorial Xav! +rep
  • 0

#3 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 07 May 2008 - 11:03 AM

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! :D
  • -1
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 07 May 2008 - 02:59 PM

If you post enough tutorials like this you'll soon have more than him!
  • 0

#5 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 08 May 2008 - 11:32 AM

He won't really like that, will he?

Hang on a minute... GREAT!
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#6 Keuvie

Keuvie

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 28 July 2008 - 03:50 AM

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
  • 0

#7 Core2quad

Core2quad

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 28 July 2008 - 06:37 AM

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.
  • 0

#8 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 28 July 2008 - 09:42 AM

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?

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?

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?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#9 Keuvie

Keuvie

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 28 July 2008 - 10:03 AM

...
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 :P
  • 0

#10 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 28 July 2008 - 10:24 AM

You only need to change one line:

Replace this:
StreamWriter sw = new StreamWriter(path)

... with this:
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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#11 Keuvie

Keuvie

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 28 July 2008 - 10:33 AM

Thanks!
  • 0

#12 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 28 July 2008 - 10:42 AM

You're welcome, I'm here to help. :)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Also tagged with one or more of these keywords: streamwriter

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download