STREAM NOT WRITING ON A FILE

Jayden Bell

Total Post:110

Points:774
Posted by  Jayden Bell
C# 
C#
 916  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. Pravesh Singh

    Post:412

    Points:2888
    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

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!