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 ");
Home / DeveloperSection / Forums / Stream not writing on a file
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
29-Jan-2014The 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().