无法写入已关闭的TextFile System.ObjectDisposedException C# [英] Cannot write to a closed TextFile System.ObjectDisposedException C#

查看:337
本文介绍了无法写入已关闭的TextFile System.ObjectDisposedException C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

我正在试图从文本文件中的相机位置保存数据,但实际上由于此错误而无法正常工作,我不知道如何更改所以它的工作原理......也许如果somebode可以帮助我,我会很幸运:)

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");
                }

推荐答案

说,不是第二个Console.SetOut ()设置为 oldOut 而不是
作家

您的最终"完成"输出也将写入文件,但流现已关闭。

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


这篇关于无法写入已关闭的TextFile System.ObjectDisposedException C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆