Closed Thread
Results 1 to 2 of 2

Thread: Why is the using statement required for StreamWriter(not the System.IO declaration)?

  1. #1
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Why is the using statement required for StreamWriter(not the System.IO declaration)?

    Why does this code not write anything (System.IO was already declared in the code earlier for both)...

    Code:
    string selection = "initialsave";
            StreamWriter sw = new StreamWriter("C:\\Program Files\\BattleGame\\" + selection + ".txt");
            sw.WriteLine("testtesttest");
    and why does this code actually write what was told to be written?

    Code:
    string selection = "initialsave";
            using (StreamWriter sw = new StreamWriter("C:\\Program Files\\BattleGame\\" + selection + ".txt"));
    {
            sw.WriteLine("testtesttest");
    }

    I was writing a game and constantly wondering why my game was never saving. I checked the game and I found that the save files were created but not written to. Why is this?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    QuackWare is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    95
    Rep Power
    8

    Re: Why is the using statement required for StreamWriter(not the System.IO declaratio

    If you don't use the "using" you have to call sw.Close() or else it wont save.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 4
    Last Post: 05-12-2011, 06:59 AM
  2. StreamWriter?
    By Colo in forum C# Programming
    Replies: 7
    Last Post: 07-08-2010, 10:42 PM
  3. StreamWriter with No Append
    By PGP_Protector in forum C# Programming
    Replies: 2
    Last Post: 06-27-2010, 08:30 AM
  4. Problem with Streamwriter
    By noxrawr in forum C# Programming
    Replies: 2
    Last Post: 06-05-2010, 11:53 PM
  5. Replies: 2
    Last Post: 05-31-2010, 02:32 PM

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