none
Cannot write to a closed TextFile System.ObjectDisposedException C# RRS feed

  • Question

  • Hey,

    Im triying to save data from a camera position in a text file, but it actually doesnt work because of this error and I have no idea how to change it so it works...maybe if somebode could help me I would be blessed :)

                    FileStream ostrm;
                    StreamWriter writer;
                    TextWriter oldOut = Console.Out;
                    try
                    {
                        ostrm = new FileStream("C:/Users/Joanne/Desktop/WriteLines35.txt", FileMode.Append | FileMode.OpenOrCreate , FileAccess.Write);
                        writer = new StreamWriter(ostrm);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Cannot open Redirect.txt for writing");
                        Console.WriteLine(e.Message);
                        return;
                    }
                    while (onvifPTZ != null)
                    {
                        Console.SetOut(writer);
                        Console.WriteLine("\t Act Value [" + curPan.ToString() +
                                                               "," + curTilt.ToString() +
                                                               "," + curZoom.ToString() + "]");
                        Console.WriteLine("\t Ref Value [" + newPTZRef.pan.ToString() +
                                                               "," + newPTZRef.tilt.ToString() +
                                                               "," + newPTZRef.zoom.ToString() + "]");
                        Console.WriteLine("\t Dif Value [" + dPan.ToString() +
                                                           "," + dTilt.ToString() +
                                                           "," + dZoom.ToString() + "]");
                        Console.SetOut(writer);
                        
                        writer.Close();
                        ostrm.Close();
                        Console.WriteLine("Done");
                    }

    And btw. if you see some errors in my code please just tell me so I can actually correct them :)
    • Edited by jojostein Tuesday, December 11, 2018 12:48 PM
    Tuesday, December 11, 2018 12:46 PM

All replies

  • Say, aren't the second Console.SetOut() set to oldOut instead of writer?

    Your final "Done" output is to be written to the file as well, but the stream is now closed.

    Wednesday, December 12, 2018 1:54 AM
    Answerer
  • Hi jojostein,

    Thank you for posting here.

    I test the code you provided. There is no use for oldOut.

    TextWriter oldOut = Console.Out;
    
    And on my test, put the code below out of the while loop, there would be no error.
      writer.Close();
      ostrm.Close();

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Thursday, December 13, 2018 8:17 AM
    Wednesday, December 12, 2018 7:04 AM
    Moderator
  • Thank you so much, it actually really solved my problem :)
    Wednesday, December 12, 2018 12:22 PM
  • Thank you :) yeah, I tried to change that but you are right, its oldOut. Thanks for your help :)

    Wednesday, December 12, 2018 12:23 PM
  • Hi jojostein,

    If your question has been solved, please mark the useful reply as answer to close the thread. This will make answer searching easier in the forum and be beneficial to community members.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 13, 2018 8:16 AM
    Moderator