Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

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

streamwriter stream

  • Please log in to reply
1 reply to this topic

#1 Smashbrosboy

Smashbrosboy

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 13 February 2010 - 09:37 AM

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

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?

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

#2 QuackWare

QuackWare

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 88 posts

Posted 14 February 2010 - 05:55 PM

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





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