1

Currently i am working on my application auto update feature. So when i am trying to delete the folder, throwing exception that used by another process. So i am planning to check if folder used by another process, i will skip the auto update feature. How to check a folder is being used by another process. I am getting exception while using below code:

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;
    }
rosh_021
  • 23
  • 1
  • 9
  • 2
    There´s no way to check this as folders are nothing but a collection of *files*. So you should check if any *file* within your folder is in use. – MakePeaceGreatAgain Sep 26 '17 at 08:32
  • @HimBromBeere there is a way, for example you can open first file, and check can you open or not. – SᴇM Sep 26 '17 at 08:34
  • @sem Isn´t that exactly what I wrote in the second sentence? – MakePeaceGreatAgain Sep 26 '17 at 08:36
  • @HimBromBeere: So, if I create a folder, with no files in it, open it through an explorer, and try to delete it, I will succeed? – Stefan Sep 26 '17 at 08:37
  • The exception thrown is a good indication that a folder is in use. – Stefan Sep 26 '17 at 08:39
  • @HimBromBeere you've wrote 2 sentences, contradicting each other. – SᴇM Sep 26 '17 at 08:40
  • I disagree with the duplicate. Folders are different than files and as far as I can tell a solution to this problem is not easy to be found in one of the duplicates answers. – Stefan Sep 26 '17 at 08:43
  • @HimBromBeere: try to open it in a `cmd.exe`, I think your folder will be locked. – Stefan Sep 26 '17 at 08:46
  • 2
    It's pretty futile to check if it's in use, in the space between you checking that it's in use and you actually going to delete it the state may change. Just try to delete it and catch and handle specifically the IOException for it being in use and abort the update. – Equalsk Sep 26 '17 at 08:50
  • Hi to all - Actually problem is my folder contain DLLs. Few DLL is not been used by another process but few are used by another process, so in that case how i can determine directly from folder level that process is been used. Note: 1. I can't use file in that folder to determine that process. 2. When i delete that folder, few files get deleted from that folder but few are left as it is used by another process. – rosh_021 Sep 26 '17 at 08:56
  • Thanks everyone. It is solved by answer provided in https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use – rosh_021 Sep 26 '17 at 09:25
  • 2
    @HimBromBeere, Windows allows processes to hold locks on folders as well as files. For example, you can't remove a directory that is the current directory for any process. – Harry Johnston Sep 26 '17 at 09:26

0 Answers0