• 错误提示:文件“D:\Log\log20170317.txt”正由另一进程使用,因此该进程无法访问该文件。

    FileStream fs = File.OpenRead(url);
    StreamReader sr = new StreamReader((Sys
    tem.IO.Stream)fs, System.Text.Encoding.Default);
  • 错误提示:文件“D:\Log\log20170317.txt”正由另一进程使用,因此该进程无法访问该文件。

    StreamReader sr = File.OpenText(url);
  • 错误提示:文件“D:\Log\log20170317.txt”正由另一进程使用,因此该进程无法访问该文件。

    string textInfo = File.ReadAllText(filePath,Encoding.Default);
  • 正确读取方式

    FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
    StringBuilder sb = new StringBuilder();
    while (!sr.EndOfStream)
    {
        sb.AppendLine(sr.ReadLine()+"\n");
    }
  • 总结:这样的情况,不单要与只读方式打开txt文件,而且,需要共享锁。还必须要选择flieShare方式为ReadWrite。因为随时有其他程序对其进行写操作。