none
A werid question about FileStream VS StreamWriter RRS feed

  • Question

  • Hello,

    When coding in my C# project, Here is a a werid question about FileStream VS StreamWriter. 

    FileStream fs=new FileStream("Temp.Dat", FileMode.Create);
    
    StreamWriter sw=new StreamWriter(fs);
    sw.Write("Hi");
    sw.Close();
    

    Do we need fs.Close()? If yes, why ? if no, why?

    Sunday, February 21, 2016 7:36 AM

Answers

  • If you intend to write more text to this file stream via fs variable (using another StreamWriter, for example), then you do not need fs.Close().

    Otherwise you should call fs.Close(), especially if you need to use the “Temp.Dat” file again (to read the text, for example).

    To close the objects automatically, usually the using statement is more suitable.

    Sunday, February 21, 2016 10:37 AM
  • In addition to Viorel_'s answer, note that there is a version of the StreamWriter constructor that allows you to specify that the underlying Steam is to be closed when the StreamWriter is closed. 
    Sunday, February 21, 2016 1:54 PM
  • HI GreeeeeeeeeeeeeD,

    When you’re finished writing data via the StreamWriter object, you should call Dispose. (Because the StreamWriter type implements the IDisposable interface, you can also use it with C#’s using statement.) This causes the StreamWriter object to flush its data to the Stream object and close the Stream object.

    Do we need fs.Close()? If yes, why ? if no, why?

    No,  Note You don’t have to explicitly call Dispose on the FileStream object because the StreamWriter calls it for you. However, if you do call Dispose explicitly, the FileStream will see that the object has already been cleaned up—the method does nothing and just returns.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 22, 2016 2:02 AM

All replies

  • If you intend to write more text to this file stream via fs variable (using another StreamWriter, for example), then you do not need fs.Close().

    Otherwise you should call fs.Close(), especially if you need to use the “Temp.Dat” file again (to read the text, for example).

    To close the objects automatically, usually the using statement is more suitable.

    Sunday, February 21, 2016 10:37 AM
  • In addition to Viorel_'s answer, note that there is a version of the StreamWriter constructor that allows you to specify that the underlying Steam is to be closed when the StreamWriter is closed. 
    Sunday, February 21, 2016 1:54 PM
  • You should always call the Dispose() method on objects that implements the IDisposable interface as soon as you are done using them. The easiest way to do this is to wrap the creation of the object in a using statement like this:

    using (FileStream fs = new FileStream("Temp.Dat", FileMode.Create))
                {
    
                }
    

    Then the object will be disposed (and closed) as soon as you leave the using the block.

    The other option is to call the Dispose method explicitly:

    FileStream fs = new FileStream("Temp.Dat", FileMode.Create);
                //...
                fs.Dispose();


    The Close() method is really just a wrapper around Dispose() with a more "friendly" name, i.e. it calls the Dispose() method internally.

    But you should always call either the Dispose method (or use a using block) or the Close method as soon as you are done reading from or writing to the file in order to release the file handles and free up the managed resources that are used under the hood to access the file.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Sunday, February 21, 2016 7:28 PM
  • HI GreeeeeeeeeeeeeD,

    When you’re finished writing data via the StreamWriter object, you should call Dispose. (Because the StreamWriter type implements the IDisposable interface, you can also use it with C#’s using statement.) This causes the StreamWriter object to flush its data to the Stream object and close the Stream object.

    Do we need fs.Close()? If yes, why ? if no, why?

    No,  Note You don’t have to explicitly call Dispose on the FileStream object because the StreamWriter calls it for you. However, if you do call Dispose explicitly, the FileStream will see that the object has already been cleaned up—the method does nothing and just returns.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 22, 2016 2:02 AM