Using C#, how does one figure out what process locked a file?

前端 未结 9 1962
死守一世寂寞
死守一世寂寞 2020-11-22 13:03

In Windows, how do I determine (using C#) what process locked a file?

Third-party tools are helpful, but not what I\'m looking for.

相关标签:
9条回答
  • 2020-11-22 13:27

    I rewrote the GetProcessesLockingFile() method in the solution. The code was not working.

    For example, you have a folder "C:\folder1\folder2" and a process in folder2 (process1). If the process was running, GetProcessesLockingFile() was returning "C:\folder1\folder2". So the condition if (files.Contains(filePath)) => if ("C:\folder1\folder2".contains("C:\folder1\folder2\process1")) was never true.

    So this is my solution:

    public static List<Process> GetProcessesLockingFile(FileInfo file)
    {
        var procs = new List<Process>();
    
        var processListSnapshot = Process.GetProcesses();
        foreach (var process in processListSnapshot)
        {
            if (process.Id <= 4) { continue; } // system processes
    
            List<string> paths = GetFilesLockedBy(process);
            foreach (string path in paths)
            {
                string pathDirectory = path;
                if (!pathDirectory.EndsWith(Constants.DOUBLE_BACKSLASH))
                {
                    pathDirectory = pathDirectory + Constants.DOUBLE_BACKSLASH;
                }
                string lastFolderName = Path.GetFileName(Path.GetDirectoryName(pathDirectory));
    
                if (file.FullName.Contains(lastFolderName))
                {
                    procs.Add(process);
                }
            }
        }
        return procs;
    }
    

    Or with a string parameter:

    public static List<Process> GetProcessesLockingFile(string filePath)
    {
        var procs = new List<Process>();
    
        var processListSnapshot = Process.GetProcesses();
        foreach (var process in processListSnapshot)
        {
            if (process.Id <= 4) { continue; } // system processes
    
            List<string> paths = GetFilesLockedBy(process);
            foreach (string path in paths)
            {
                string pathDirectory = path;
                if (!pathDirectory.EndsWith(Constants.DOUBLE_BACKSLASH))
                {
                    pathDirectory = pathDirectory + Constants.DOUBLE_BACKSLASH;
                }
                string lastFolderName = Path.GetFileName(Path.GetDirectoryName(pathDirectory));
    
                if (filePath.Contains(lastFolderName))
                {
                    procs.Add(process);
                }
            }
        }
        return procs;
    }
    
    0 讨论(0)
  • 2020-11-22 13:30

    You absolutely don't need to run in Kernel mode (!!!)
    It's a Win32 FAQ since Windows 95 (!) (in C, Google groups, Win32) : read the handle table, from User mode of course, and get the PID from the File handle ...

    0 讨论(0)
  • 2020-11-22 13:33

    Handle, from Windows Sysinternals. This is a free command-line utility provided by Microsoft.

    You could run it, and parse the result.

    0 讨论(0)
  • 2020-11-22 13:34

    This question had an original answer that is now over 7 years old. That code is preserved at https://gist.github.com/i-e-b/2290426 This old version might work for you if you need to use Windows XP for some reason.

    A much better answer is at How to check for file lock?

    I've replicated Eric J's answer below (with using statements added, and class & method names to match the old code that was here) Please note that the comments to this answer may be out-of-date.

    Research by user 'Walkman' is ongoing to improve the older code, as there are some conditions where the Restart Manager does not list all locks. See Github repo: https://github.com/Walkman100/FileLocks

    Use like:

    List<Process> locks = Win32Processes.GetProcessesLockingFile(@"C:\Hello.docx");
    

    Code:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    namespace FileLockInfo
    {
        public static class Win32Processes
        {
            /// <summary>
            /// Find out what process(es) have a lock on the specified file.
            /// </summary>
            /// <param name="path">Path of the file.</param>
            /// <returns>Processes locking the file</returns>
            /// <remarks>See also:
            /// http://msdn.microsoft.com/en-us/library/windows/desktop/aa373661(v=vs.85).aspx
            /// http://wyupdate.googlecode.com/svn-history/r401/trunk/frmFilesInUse.cs (no copyright in code at time of viewing)
            /// </remarks>
            public static List<Process> GetProcessesLockingFile(string path)
            {
                uint handle;
                string key = Guid.NewGuid().ToString();
                int res = RmStartSession(out handle, 0, key);
    
                if (res != 0) throw new Exception("Could not begin restart session.  Unable to determine file locker.");
    
                try
                {
                    const int MORE_DATA = 234;
                    uint pnProcInfoNeeded, pnProcInfo = 0, lpdwRebootReasons = RmRebootReasonNone;
    
                    string[] resources = {path}; // Just checking on one resource.
    
                    res = RmRegisterResources(handle, (uint) resources.Length, resources, 0, null, 0, null);
    
                    if (res != 0) throw new Exception("Could not register resource.");
    
                    //Note: there's a race condition here -- the first call to RmGetList() returns
                    //      the total number of process. However, when we call RmGetList() again to get
                    //      the actual processes this number may have increased.
                    res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, null, ref lpdwRebootReasons);
    
                    if (res == MORE_DATA)
                    {
                        return EnumerateProcesses(pnProcInfoNeeded, handle, lpdwRebootReasons);
                    }
                    else if (res != 0) throw new Exception("Could not list processes locking resource. Failed to get size of result.");
                }
                finally
                {
                    RmEndSession(handle);
                }
    
                return new List<Process>();
            }
    
    
            [StructLayout(LayoutKind.Sequential)]
            public struct RM_UNIQUE_PROCESS
            {
                public int dwProcessId;
                public System.Runtime.InteropServices.ComTypes.FILETIME ProcessStartTime;
            }
    
            const int RmRebootReasonNone = 0;
            const int CCH_RM_MAX_APP_NAME = 255;
            const int CCH_RM_MAX_SVC_NAME = 63;
    
            public enum RM_APP_TYPE
            {
                RmUnknownApp = 0,
                RmMainWindow = 1,
                RmOtherWindow = 2,
                RmService = 3,
                RmExplorer = 4,
                RmConsole = 5,
                RmCritical = 1000
            }
    
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public struct RM_PROCESS_INFO
            {
                public RM_UNIQUE_PROCESS Process;
    
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_APP_NAME + 1)] public string strAppName;
    
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_SVC_NAME + 1)] public string strServiceShortName;
    
                public RM_APP_TYPE ApplicationType;
                public uint AppStatus;
                public uint TSSessionId;
                [MarshalAs(UnmanagedType.Bool)] public bool bRestartable;
            }
    
            [DllImport("rstrtmgr.dll", CharSet = CharSet.Unicode)]
            static extern int RmRegisterResources(uint pSessionHandle, uint nFiles, string[] rgsFilenames,
                uint nApplications, [In] RM_UNIQUE_PROCESS[] rgApplications, uint nServices,
                string[] rgsServiceNames);
    
            [DllImport("rstrtmgr.dll", CharSet = CharSet.Auto)]
            static extern int RmStartSession(out uint pSessionHandle, int dwSessionFlags, string strSessionKey);
    
            [DllImport("rstrtmgr.dll")]
            static extern int RmEndSession(uint pSessionHandle);
    
            [DllImport("rstrtmgr.dll")]
            static extern int RmGetList(uint dwSessionHandle, out uint pnProcInfoNeeded,
                ref uint pnProcInfo, [In, Out] RM_PROCESS_INFO[] rgAffectedApps,
                ref uint lpdwRebootReasons);
    
            private static List<Process> EnumerateProcesses(uint pnProcInfoNeeded, uint handle, uint lpdwRebootReasons)
            {
                var processes = new List<Process>(10);
                // Create an array to store the process results
                var processInfo = new RM_PROCESS_INFO[pnProcInfoNeeded];
                var pnProcInfo = pnProcInfoNeeded;
    
                // Get the list
                var res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, processInfo, ref lpdwRebootReasons);
    
                if (res != 0) throw new Exception("Could not list processes locking resource.");
                for (int i = 0; i < pnProcInfo; i++)
                {
                    try
                    {
                        processes.Add(Process.GetProcessById(processInfo[i].Process.dwProcessId));
                    }
                    catch (ArgumentException) { } // catch the error -- in case the process is no longer running
                }
                return processes;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 13:38

    I believe that you need code running in kernel mode to completely answer the question (but I haven't looked at the restart manager API).

    You can enumerate all processes and their modules - so if the file you're looking for is a module (DLL, EXE, OCX...), you're good to go. But if it's a text file for example, you have to look at the kernel handle table which you cannot see from user mode. Handle.exe has a kernel driver in order to do that.

    0 讨论(0)
  • 2020-11-22 13:48

    Try Unlocker. If you try and delete the file that is locked by another process, it will list the process(es) that have the file locked. You can then unlock the file by shutting down those processes.

    0 讨论(0)
提交回复
热议问题