c#如何检查另一个进程正在使用的文件夹

时间:2017-09-26 08:31:04

标签: c# .net windows file io

目前我正在处理我的应用程序自动更新功能。因此,当我尝试删除该文件夹时,抛出另一个进程使用的异常。所以我打算检查另一个进程使用的文件夹,我将跳过自动更新功能。如何检查另一个进程正在使用的文件夹。我在使用下面的代码时遇到异常:

private static ArrayList getFileProcesses(string strFile)
    {
        myProcessArray.Clear();
        Process[] processes = Process.GetProcesses();
        int i = 0;
        for (i = 0; i <= processes.GetUpperBound(0) - 1; i++)
        {
            myProcess = processes[i];
            //if (!myProcess.HasExited) //This will cause an "Access is denied" error
            if (myProcess.Threads.Count > 0)
            {
                try
                {
                    ProcessModuleCollection modules = myProcess.Modules;
                    int j = 0;
                    for (j = 0; j <= modules.Count - 1; j++)
                    {
                        if ((modules[j].FileName.ToLower().CompareTo(strFile.ToLower()) == 0))
                        {
                            myProcessArray.Add(myProcess);
                            break;
                            // TODO: might not be correct. Was : Exit For
                        }
                    }
                }
                catch (Exception exception)
                {
                    //MsgBox(("Error : " & exception.Message)) 
                }
            }
        }

        return myProcessArray;
    }

0 个答案:

没有答案
相关问题