Home > DeveloperSection > Forums > Stream not writing on a file
Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    January-28-2014 11:47 PM

 C# C# 
Ratings:


 1 Reply(s)
 676  View(s)
Rate this:

Is there anything wrong with this piece of code ? It's opening the file , yet it doesn't write on it.

fl = new FileStream(path, FileMode.OpenOrCreate);

sw = new StreamWriter(fl);

sw.WriteLine("Hello ");


Pravesh Singh

Total Post:411

Points:2881
Posted on    January-29-2014 12:46 AM

Hi Jayden,

The proper way of doing this would be:

using (StreamWriter sw = new StreamWriter(new FileStream(path, FileMode.OpenOrCreate)))

    sw.WriteLine("Hello ");

This will call sw.Flush(), sw.Close() and sw.Dispose().


Don't want to miss updates? Please click the below button!

Follow MindStick