C# 读取文本文件而不锁定它

C# 读取文本文件而不锁定它,c#,C#,我有一个应用程序(不是我写的,不能修改代码),它以两种模式运行-基于GUI,或静默。它将其进度写入日志文件。我编写了一个包装器,将其转换为伪命令行应用程序,使用System.Diagnostics.FileSystemWatcher监视日志文件,并且在日志文件发生更改时,将最新的一行写入控制台: private static void OnChanged(object source, FileSystemEventArgs e) { Console.WriteLine(File.Read

我有一个应用程序(不是我写的,不能修改代码),它以两种模式运行-基于GUI,或静默。它将其进度写入日志文件。我编写了一个包装器,将其转换为伪命令行应用程序,使用
System.Diagnostics.FileSystemWatcher
监视日志文件,并且在日志文件发生更改时,将最新的一行写入控制台:

private static void OnChanged(object source, FileSystemEventArgs e)
{
    Console.WriteLine(File.ReadAllLines(e.FullPath).Last());
}
这工作得很好,直到日志文件连续两次快速更新,应用程序突然发出错误消息:
System.IO.IOException:进程无法访问文件'\Upgrader.log',因为另一个进程正在使用它。

如何将日志条目重定向到控制台,而不锁定文件?我已经找到了答案,但我不确定如何使用文件流来获得相同的效果

编辑:

因此,如果我有一个dict来跟踪日志文件:

Dictionary<string, FileStream> logDict = new Dictionary<string, FileStream>();
Dictionary<string, int> indexDict = new Dictionary<string, int>();
然后改变一次,变成

private static void OnChanged(object source, FileSystemEventArgs e)
{
    using (StreamReader sr = new StreamReader(logDict[e.FullPath]))
    {
        for (int i = 0; i < indexDict[e.FullPath]; i++)
            sr.ReadLine();
        Console.WriteLine(sr.ReadLine());
        indexDict[FullPath]++;
        sr.Close();
    }
}
private static void OnChanged(对象源、文件系统目标)
{
使用(StreamReader sr=新的StreamReader(logDict[e.FullPath]))
{
for(int i=0;i
那应该行吗

再次编辑:我想我有一个解决方案。尝试过这个,它似乎起了作用:

Dictionary<string, int> indexDict = new Dictionary<string, int>();

private static void OnChanged(object source, FileSystemEventArgs e)
{
    if (!indexDict.Keys.Contains(e.FullPath))
        indexDict[e.FullPath] = 0;
    using (FileStream fs = new FileStream(e.Full Path,
                                          FileMode.Open,
                                          FileAccess.Read,
                                          FileShare.ReadWrite))
    {
        using (StreamReader sr = new StreamReader(fs))
        {
            for (int i = 0; i < indexDict[e.FullPath]; i++)
                sr.ReadLine();
            Console.WriteLine(sr.ReadLine());
            indexDict[e.FullPath]++;
            sr.Close();
        }
    }
}
Dictionary indexDict=newdictionary();
私有静态void OnChanged(对象源、文件系统目标)
{
如果(!indexDict.Keys.Contains)(如完整路径))
indexDict[e.FullPath]=0;
使用(FileStream fs=newfilestream(例如,完整路径,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite)
{
使用(StreamReader sr=新StreamReader(fs))
{
for(int i=0;i
副本准确地描述了您应该做什么,包括代码和所有内容。我们可以复制代码,但这没有用。解释您在理解方面遇到的困难。根据我从另一个答案中了解的内容,对建议的解决方案进行编辑。我走对了吗?我不知道。你为什么要把它们放在字典里。完成后只需打开并关闭它们。我在字典中也看不到任何值。所以每次事件触发时都要重新打开FileStream吗?
Dictionary<string, int> indexDict = new Dictionary<string, int>();

private static void OnChanged(object source, FileSystemEventArgs e)
{
    if (!indexDict.Keys.Contains(e.FullPath))
        indexDict[e.FullPath] = 0;
    using (FileStream fs = new FileStream(e.Full Path,
                                          FileMode.Open,
                                          FileAccess.Read,
                                          FileShare.ReadWrite))
    {
        using (StreamReader sr = new StreamReader(fs))
        {
            for (int i = 0; i < indexDict[e.FullPath]; i++)
                sr.ReadLine();
            Console.WriteLine(sr.ReadLine());
            indexDict[e.FullPath]++;
            sr.Close();
        }
    }
}