Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 进程无法访问文件“XXX”,因为另一进程正在使用该文件_C#_File_File Io - Fatal编程技术网

C# 进程无法访问文件“XXX”,因为另一进程正在使用该文件

C# 进程无法访问文件“XXX”,因为另一进程正在使用该文件,c#,file,file-io,C#,File,File Io,我得到以下例外 Error Message:The process cannot access the file '....\folder\r.js' 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 m

我得到以下例外

Error Message:The process cannot access the file '....\folder\r.js' 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)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
   at System.IO.StreamReader..ctor(String path, Encoding encoding)
   at System.IO.File.ReadAllLines(String path, Encoding encoding)
   at System.IO.File.ReadAllLines(String path)
   at PrepareEventDates()
我的方法:

protected HashSet<string> PrepareEventDates()
{
    HashSet<string> str = new HashSet<string>();
    WebClient webclient = new WebClient();
    string path = System.Configuration.ConfigurationSettings.AppSettings.Get("sss").ToString();
    webclient.DownloadFile(path, Server.MapPath("r.js"));
    String[] JSLines = System.IO.File.ReadAllLines(Server.MapPath("folder/r.js"));
    String strDate = string.Empty;
    string toolTip = string.Empty;
    for (int i = 0; i < JSLines.Length; i++)
    {
        string result = JSLines[i];
        int methodBodyStart = JSLines[i].IndexOf("DefineEvent(");
        if (methodBodyStart >= 0)
        {
            methodBodyStart += "DefineEvent(".Length;
            int methodBodyEnd = JSLines[i].LastIndexOf(')');
            if (methodBodyEnd >= 0)
            {
                string methodBody = JSLines[i].Substring(methodBodyStart, methodBodyEnd - methodBodyStart);
                var twoParams = methodBody.Split(',')
                   .Select(x => x.Trim(' ', '\'')).Take(2);
                result = string.Join("|", twoParams.ToArray());
            }
        }
        str.Add(result);
    }

    return str;
}

这个问题与FileShare选项有关,从StackTrace错误可以看出,您用来读取行的方法使用了FileStream对象

我建议您阅读该文件,但自己实例化文件流:

string contents = string.Empty;

using(FileStream fs = new FileStream("", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (StreamReader sr = new StreamReader(fs))
       contents = sr.ReadToEnd();

如何创建/写入文件夹/r.js文件?这里您只展示了如何从中读取:String[]JSLines=System.IO.File.ReadAllLinesServer.MapPathfolder/r.js;但通常是打开文件进行编写的代码可能写得不好并将其锁定。此外,如果这是在web应用程序中,则应同步对这些文件的访问。想象两个并行请求击中您的服务器。除非您使用ReaderWriterLockSlim来处理文件,否则您的文件很可能会被损坏。请在完成工作后尝试处置webclient。webclient与此无关。WebClient使用的是Server.MapPathr.js文件,而不是Server.MapPathfolder/r.js文件,显然这里锁定了该文件。但是,是的,一般来说,将IDisposable资源(如本例中的WebClient)封装在using语句中是一种很好的做法。请看一下我的答案。谢谢你试过了吗?成功了吗?