利用 .NET Framework 辨識鎖定檔案的進程
傳統上,找出 .NET 框架內持有檔案鎖的特定流程是一項重大挑戰。 然而,隨著現代 Windows 的進步,重新啟動管理器 API 現在提供了一種可靠的機制來追蹤此資訊。
解決方案實作:
以下程式碼片段提供了一種可靠的方法來識別已在指定檔案上建立鎖定的進程:
<code class="language-csharp">public static List<Process> IdentifyFileLockers(string filePath) { uint sessionHandle; string sessionKey = Guid.NewGuid().ToString(); List<Process> lockedByProcesses = new List<Process>(); int result = RmStartSession(out sessionHandle, 0, sessionKey); if (result != 0) throw new Exception("Failed to initiate restart session. Unable to identify file locker."); try { const int ERROR_MORE_DATA = 234; uint processesNeeded = 0, processesReturned = 0, rebootReasons = RmRebootReasonNone; string[] resources = new string[] { filePath }; // Targeting a single resource. result = RmRegisterResources(sessionHandle, (uint)resources.Length, resources, 0, null, 0, null); if (result != 0) throw new Exception("Resource registration failed."); //Note: A race condition exists here. The initial RmGetList() call returns // the total process count. Subsequent RmGetList() calls to retrieve // actual processes might encounter an increased count. result = RmGetList(sessionHandle, out processesNeeded, ref processesReturned, null, ref rebootReasons); if (result == ERROR_MORE_DATA) { // Allocate an array to store process information. RM_PROCESS_INFO[] processInfoArray = new RM_PROCESS_INFO[processesNeeded]; processesReturned = processesNeeded; // Retrieve the process list. result = RmGetList(sessionHandle, out processesNeeded, ref processesReturned, processInfoArray, ref rebootReasons); if (result == 0) { lockedByProcesses = new List<Process>((int)processesReturned); // Iterate through results and populate the return list. for (int i = 0; i < processesReturned; i++) { try { //Attempt to get the process by ID. May fail if the process is already gone. Process p = Process.GetProcessById(processInfoArray[i].Process.dwProcessId); lockedByProcesses.Add(p); } catch (ArgumentException) { } // Ignore processes that no longer exist. } } } } finally { RmEndSession(sessionHandle); } return lockedByProcesses; }</code>
重要提示:執行此程式碼需要非特權的登錄存取。如果進程缺乏必要的權限,建議實作 IPC 機制(例如命名管道)以將呼叫委託給特權程序。
以上是如何使用重新啟動管理器 API 識別在 .NET 中鎖定檔案的進程?的詳細內容。更多資訊請關注PHP中文網其他相關文章!