gpt4 book ai didi

c# - 文件流 "cannot access closed file"

转载 作者:太空狗 更新时间:2023-10-29 21:22:06 26 4
gpt4 key购买 nike

为什么我在使用 using (fileStream = new FileStream(path, FileMode.Append, FileAccess.Write)) 时收到上述错误消息,但当我将其替换为 fileStream = File.Create(path); ?

我想将控制台输出附加到外部文件。这是我的代码:

 //copy console output to file in bin\Debug folder
class ConsoleCopy : IDisposable
{
FileStream fileStream;
StreamWriter fileWriter;
TextWriter doubleWriter;
TextWriter oldOut;
class DoubleWriter : TextWriter
{
TextWriter one;
TextWriter two;
public DoubleWriter(TextWriter one, TextWriter two)
{
this.one = one;
this.two = two;
}
public override Encoding Encoding
{
get { return one.Encoding; }
}
//Clears all buffers for the current writer
public override void Flush()
{
one.Flush();

two.Flush();
}
public override void Write(char value)
{
**one.Write(value);**//error thrown here
two.Write(value);
}
}
public ConsoleCopy(string path)
{
oldOut = Console.Out;
try
{
**//fileStream = File.Create(path);** //runs alright with this line
fileWriter = new StreamWriter(fileStream);
fileWriter.AutoFlush = true;
doubleWriter = new DoubleWriter(fileWriter, oldOut);
}
catch (Exception e)
{
Console.WriteLine("Cannot open file for writing");
Console.WriteLine(e.Message);
return;
}
Console.SetOut(doubleWriter);
}
public void Dispose()
{
Console.SetOut(oldOut);
if (fileWriter != null)
{
fileWriter.Flush();
fileWriter.Close();
fileWriter = null;
}
if (fileStream != null)
{
fileStream.Close();
fileStream = null;
}
}
}//end of consoleCopy

我在我的主要方法中这样调用这个方法:

 //pass output to ConsoleCopy method for copying to .txt file
using (var cc = new ConsoleCopy("mylogfile.txt"))
{
DateTime theDate = DateTime.UtcNow;

string custom = theDate.ToString("d");

Console.WriteLine("User has logged in on " + custom);
}

更新:

该错误先前显示在 one.Write(value) 处。多亏了彼得,我已经设法解决了它。感谢大家的贡献和帮助:)

最佳答案

编辑:如果您在编写之前得到它,那是因为我误读了问题并将 using 语句放在 ConsoleCopy 构造函数中意味着当 ConsoleCopy 完成创建时,FileStream 将关闭。当您尝试写入它时,因为它已经关闭,您会得到该异常。相同的解决方案将适用 - 让 FileStream 打开并在 Dispose() 中关闭它,它应该与 main 方法中的 using 语句一起使用。

如果我没理解错的话,你会得到处理异常,对吧?如果是这样,那是因为发生的事情本质上是您关闭了该流两次。

using (var cc = new ConsoleCopy("mylogfile.txt"))
{
// At this time, your filestream is created with the using statement.
// Writing happens here.
// Your filestream is closed at the end of the using statement inside of the ConsoleCopy constructor.
}
// At this point, ConsoleCopy tries to call Dispose(), which flushes and closes everything again. However, this throws an exception because the using statement already closed the FileStream.

如果您的目标是追加到文件末尾并且您的 Dispose() 方法在内部关闭所有内容,为什么不直接替换

**//fileStream = File.Create(path);** //runs alright with this line

fileStream = File.Open(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);

并让 main 中的 using 语句在内部为您关闭流?

关于c# - 文件流 "cannot access closed file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29617289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com