+ Reply to Thread
Page 1 of 5 123 ... LastLast
Results 1 to 10 of 48

Thread: C# Tutorial: Writing Text Files

  1. #1
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    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:

    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:

    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!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: C# Tutorial: Writing Text Files

    Nice tutorial Xav! +rep

  4. #3
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    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!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  5. #4
    Jordan Guest

    Re: C# Tutorial: Writing Text Files

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

  6. #5
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C# Tutorial: Writing Text Files

    He won't really like that, will he?

    Hang on a minute... GREAT!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  7. #6
    Keuvie is offline Newbie
    Join Date
    Jul 2008
    Posts
    13
    Rep Power
    0

    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

  8. #7
    Core2quad is offline Newbie
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    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.

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    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?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #9
    Keuvie is offline Newbie
    Join Date
    Jul 2008
    Posts
    13
    Rep Power
    0

    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

  11. #10
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C# Tutorial: Writing Text Files

    You only need to change one line:

    Replace this:
    [highlight=csharp]StreamWriter sw = new StreamWriter(path)[/highlight]
    ... with this:
    [highlight=csharp]StreamWriter sw = new StreamWriter(path, True)[/highlight]
    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.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

+ Reply to Thread
Page 1 of 5 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. C# Tutorial: Reading and Writing XML Files
    By rueleonheart in forum CSharp Tutorials
    Replies: 3
    Last Post: 07-19-2011, 12:02 PM
  2. C++ Writing Binary Files?
    By AtoZ in forum C and C++
    Replies: 1
    Last Post: 01-31-2010, 11:14 AM
  3. Help With Writing to Text files
    By teensicle in forum C and C++
    Replies: 5
    Last Post: 01-24-2010, 11:19 AM
  4. Differences between text files and binary files.
    By LoneWolf in forum C and C++
    Replies: 3
    Last Post: 02-24-2009, 04:36 PM
  5. Writing Config Files
    By mevets in forum Pascal and Delphi
    Replies: 0
    Last Post: 10-15-2007, 07:21 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts