【发布时间】:2014-12-11 22:02:30
【问题描述】:
这是here 问题的续集,我想知道为什么我的流无法涉水。
利用回答另一个问题的一些猫的想法,我现在得到了这个代码:
private readonly FileStream _fileStream;
private readonly StreamWriter _streamWriter;
. . .
private ExceptionLoggingService()
{
const int MAX_LINES_DESIRED = 1000;
int linesInLogFile;
string uriPath = GetExecutionFolder() + "\\Application.log";
string logPath = new Uri(uriPath).LocalPath;
_fileStream = File.Open(logPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader _streamReader = new StreamReader(_fileStream);
List<String> logList = new List<String>();
while (!_streamReader.EndOfStream)
{
logList.Add(_streamReader.ReadLine());
}
linesInLogFile = logList.Count;
while (logList.Count > MAX_LINES_DESIRED)
{
logList.RemoveAt(0);
}
if (linesInLogFile > MAX_LINES_DESIRED)
{
_fileStream.Close();
File.Delete(logPath);
File.Create(logPath);
_fileStream.Close(); // added this; did not help
_fileStream.Dispose(); // this also did no good
_fileStream = File.OpenWrite(logPath); // <= exception occurs here
}
_streamWriter = new StreamWriter(_fileStream);
foreach (String s in logList)
{
_streamWriter.WriteLine(s);
}
_streamWriter.Flush(); // here is okay, right (as opposed to within the foreach loop)?
}
...但是在指示的 ("OpenWrite()") 行上,我得到以下异常(我在它上面添加了两行,首先调用 Close(),然后调用 Dispose(),但异常仍然是同样):
System.IO.IOException was unhandled
_HResult=-2147024864
_message=The process cannot access the file 'C:\HoldingTank\Sandbox\bin\Debug\Application.log' because it is being used by another process.
那么如果Close没有关闭_fileStream,而Dispose也没有dispose它,那该怎么办呢?
更新
这并没有严格回答我的问题,但受劳埃德评论的启发,它确实有效:
const int MAX_FILESIZE_ALLOWED = 20000;
string uriPath = GetExecutionFolder() + "\\Application.log";
string logPath = new Uri(uriPath).LocalPath;
FileInfo f = new FileInfo(logPath);
long fileLenInBytes = f.Length;
if (fileLenInBytes > MAX_FILESIZE_ALLOWED)
{
File.Delete(logPath);
}
_fileStream = File.OpenWrite(logPath);
_streamWriter = new StreamWriter(_fileStream);
【问题讨论】:
-
想法很简单。既然您想更新您的
Application.log文件(参考:stackoverflow.com/questions/27429716/…),请不要尝试同时读取和写入它。读取您的文件,关闭它,然后写入结果。这不是魔法。 -
是的,这个想法很简单;暗黑破坏神在细节中。
-
问了这么多题为什么还是不能解决是因为你的无能,而不是因为cmet或到目前为止发布的答案。例如。我认为我之前的评论是对您问题的确切答案。 (你知道如何实现它吗?然后阅读基本的 c# 教程。)
-
也许是这样,但我脑力的不足我靠坚持来弥补。
-
@B.ClayShannon 实际上,根据之前评论的清晰度 - 看起来你两个都很好:)
标签: c# file-io process filestream