C#文件正被另一个进程使用 [英] C# file is being used by another process

查看:174
本文介绍了C#文件正被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何解决我的问题。有时我得到错误:
进程无法访问文件xxxx,因为它正在被另一个进程使用。



这里是我的方法发生错误︰

  private static void write_history(int index,int time_in_sec,int [] sent_resources)
{
string filepath =Config\\xxx.txt;
int writing_index = 0;

if(File.Exists(filepath))
{
System.Threading.Thread.Sleep(5000);
StreamReader reader = new StreamReader(new FileStream(filepath,FileMode.Open,FileAccess.Read,FileShare.Read));
string temp = reader.ReadToEnd();
reader.Close(); (int i = 0; i <20; i ++)

if(temp.IndexOf(< hst_+ i.ToString()+ >)== -1)
{
writing_index = i;
break;
}
}
}

System.Threading.Thread.Sleep(5000);
//写入文件
StreamWriter writer = new StreamWriter(filepath,true);
writer.WriteLine(< hst_+ writing_index.ToString()+>+ DateTime.Now.AddSeconds(time_in_sec).ToString()+|+ sent_resources [0] +| + sent_resources [1] +|+ sent_resources [2] +|+ sent_resources [3] +< / hst_+ writing_index.ToString()+>);
writer.Close();
}

和我得到的错误:

  **************例外文字************** 
系统。 IO.IOException:进程无法访问Config \\xxx.txt文件,因为正在被另一个进程使用。 $ System.IO
。Error.WinIOError(Int32 errorCode,String maybeFullPath)$ b $在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权利,布尔useRights,FileShare共享, Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,String msgPath,布尔bFromProxy,布尔useLongPath)


解决方案

如果您确定您确实正确地打开和关闭文件,最可能的罪魁祸首是您的病毒检测器。病毒检测器是臭名昭着的,因为它观察到日志文件已经更改,将其打开以搜索病毒,然后在病毒检查程序正在读取的过程中尝试写入文件失败。如果是这样的话,我会问你的病毒检查厂商他们推荐的解决方法是什么。


I am not sure how can I solve my problem. From time to time I get the error: "The process cannot access the file 'xxxx' because it is being used by another proces".

Here is my method where the error happens:

private static void write_history(int index, int time_in_sec, int[] sent_resources)
        {
            string filepath = "Config\\xxx.txt";
            int writing_index = 0;

            if (File.Exists(filepath))
            {
                System.Threading.Thread.Sleep(5000);
                StreamReader reader = new StreamReader(new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read));
                string temp = reader.ReadToEnd();
                reader.Close();

                for (int i = 0; i < 20; i++)
                {
                    if (temp.IndexOf("<hst_" + i.ToString() + ">") == -1)
                    {
                        writing_index = i;
                        break;
                    }
                }
            }

            System.Threading.Thread.Sleep(5000);
            // write to the file
            StreamWriter writer = new StreamWriter(filepath, true);
            writer.WriteLine("<hst_" + writing_index.ToString() + ">" + DateTime.Now.AddSeconds(time_in_sec).ToString() + "|" + sent_resources[0] + "|" + sent_resources[1] + "|" + sent_resources[2] + "|" + sent_resources[3] + "</hst_" + writing_index.ToString() + ">");
            writer.Close();
        }

And the error I get:

************** Exception Text **************
System.IO.IOException: The process cannot access the file 'Config\\xxx.txt' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)

解决方案

If you have made sure that you are genuinely opening and closing the file correctly, the most likely culprit is your virus detector. Virus detectors are notorious for observing that a log file has changed, opening it up to search it for a virus, and then while it is being read by the virus checker, an attempt to write to the file fails.

If that's the case, then I would ask the vendor of your virus checker what their recommended workaround is.

这篇关于C#文件正被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆