How do I find out which process is locking a file using .NET?

前端 未结 6 975
渐次进展
渐次进展 2020-11-22 00:42

I\'ve seen several of answers about using Handle or Process Monitor, but I would like to be able to find out in my own code (C#) which process is locking a file.

I ha

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 01:14

    This works for DLLs locked by other processes. This routine will not find out for example that a text file is locked by a word process.

    C#:

    using System.Management; 
    using System.IO;   
    
    static class Module1 
    { 
    static internal ArrayList myProcessArray = new ArrayList(); 
    private static Process myProcess; 
    
    public static void Main() 
    { 
    
        string strFile = "c:\\windows\\system32\\msi.dll"; 
        ArrayList a = getFileProcesses(strFile); 
        foreach (Process p in a) { 
            Debug.Print(p.ProcessName); 
        } 
    } 
    
    
    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) { 
                try { 
                    ProcessModuleCollection modules = myProcess.Modules; 
                    int j = 0; 
                    for (j = 0; j <= modules.Count - 1; j++) { 
                        if ((modules.Item(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; 
    } 
    } 
    

    VB.Net:

    Imports System.Management
    Imports System.IO
    
    Module Module1
    Friend myProcessArray As New ArrayList
    Private myProcess As Process
    
    Sub Main()
    
        Dim strFile As String = "c:\windows\system32\msi.dll"
        Dim a As ArrayList = getFileProcesses(strFile)
        For Each p As Process In a
            Debug.Print(p.ProcessName)
        Next
    End Sub
    
    
    Private Function getFileProcesses(ByVal strFile As String) As ArrayList
        myProcessArray.Clear()
        Dim processes As Process() = Process.GetProcesses
        Dim i As Integer
        For i = 0 To processes.GetUpperBound(0) - 1
            myProcess = processes(i)
            If Not myProcess.HasExited Then
                Try
                    Dim modules As ProcessModuleCollection = myProcess.Modules
                    Dim j As Integer
                    For j = 0 To modules.Count - 1
                        If (modules.Item(j).FileName.ToLower.CompareTo(strFile.ToLower) = 0) Then
                            myProcessArray.Add(myProcess)
                            Exit For
                        End If
                    Next j
                Catch exception As Exception
                    'MsgBox(("Error : " & exception.Message))
                End Try
            End If
        Next i
        Return myProcessArray
    End Function
    End Module
    

提交回复
热议问题