-3
我已經構建了File System Watcher解決方案,並且我將檢索對日誌文件所做的更改,只提取添加的行,但我不能在這個,因爲我總是錯誤,該進程無法訪問該文件,因爲它正在被另一個進程使用。 我的代碼是這樣的檢索添加的行是這樣的:該進程無法訪問該文件,因爲它正在被另一個進程使用時,我計數
using (var file = new FileStream(FileName,
FileMode.Open,
FileAccess.Read,
FileShare.Read))
using (var sr = new StreamReader(file))
{
try
{
Thread thread = new Thread(delegate()
{
listBox1.Invoke((MethodInvoker)(() =>
{
listBox1.Items.Clear();//this is the listbox that list every added line and that I clear before of to be filled
string[] lines = File.ReadLines(fileName)
.Skip(LinecountStartPositionBUFF)
.Take(LinecountEndPosition - LinecountStartPositionBUFF)
.ToArray();
listBox1.Items.AddRange(lines);
}));
});
thread.Start();
while (thread.IsAlive)
Application.DoEvents();
}
catch
{ }
return sr.ReadLine();
}
在您繼續之前,請重命名您的控件。 ('listbox1'是**不是一個好的命名約定)。順便說一句,也沒有使用空的「catch {}」塊 – jbutler483
與您的問題無關,但吞嚥異常是不好的做法。添加處理您的catch塊 – apomene
請參閱[讀取文本文件的最後一行](http://stackoverflow.com/questions/11625595/read-last-line-of-text-file)以獲得正確的實現。你的代碼有很多錯誤。 – CodeCaster