Stream not writing on a file

Total Post:110

Points:774

C# 
C#
 1238  View(s)
Ratings:
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 ");
  1. Re: Stream not writing on a file

    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().

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.