【问题标题】:The process can not access the file because this file is used by another [duplicate]该进程无法访问该文件,因为该文件被另一个[重复]
【发布时间】:2017-01-03 19:44:15
【问题描述】:

我有一个 file.log 不断写入,我用一些脚本将此文件复制到我的桌面,我测试 a 关键字是否在日志的最后一个通道上,如果是,我显示绿色图片,如果不是,我显示红色图片问题是当我启动程序时出现此错误,她是代码

    {

        // File.ReadAllLines(@"C:\\Users\\Reta\\Desktop\\TEST\\TEST\\fichiers\\k20\\winvsrTEST.log").Last();
        // System.IO.StreamReader file = new System.IO.StreamReader(@"C:\\Users\\Reta\\Desktop\\TEST\\TEST\\fichiers\\k20\\winvsrTEST.log");
        string motcle1 = "oee code";
        //string line = File.ReadLine().Last().ToString();
        var lines = File.ReadAllLines(@"C:\Users\Reta\Desktop\TEST\TEST\fichiers\k20\winvsrTEST.log");
        string line = lines.Last();


        //line = File.ReadAllLine();
        //do
        {
            if (line.Contains(motcle1))
            {

                pictureBox2.Show();
                pictureBox1.Hide();


            }
            else
            {
                pictureBox2.Hide();
                pictureBox1.Show();
            }


        }
        //while ((line = File.ReadLine()) != null);
        label1.Text = "Hi";

    }
}

}`

【问题讨论】:

  • 试试Using()。看起来你的进程没有处理,所以它仍然阻止你的文件。
  • 这也取决于日志文件是独占打开还是允许其他人从中读取..
  • 它允许其他人从中读取

标签: c#


【解决方案1】:

尝试关注...

FileStream s2 = new FileStream("FileName", FileMode.Open, FileAccess.Read, FileShare.Read);
        StreamReader sr = new StreamReader(s2);

        while (!sr.EndOfStream)
        {
            string line;
            if ((line = sr.ReadLine()) != null)
            {
                //do your work here
            }
        }

【讨论】:

  • 尝试解释原因。
  • @Rudra,在回答有关 Stack Overflow 的问题时,最好提供一个简短的描述,描述您的代码究竟是做什么的,或者为什么这样做,以便其他人更容易阅读并理解为什么您的代码有效。发布指向方法签名定义的链接通常也很有用,即msdn.microsoft.com/en-us/library/5h0z48dh(v=vs.110).aspx
  • hmm...因为您正试图以读/写模式打开文件。您需要以读取模式打开文件。
  • 在 stackoverflow 上找到相同的问题。 stackoverflow.com/questions/12744725/…
  • 文件流是否允许我读取每个通道的日志通道并使用最后一个通道?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
相关资源
最近更新 更多