Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This works for dlls locked by other processes. This routine will not find out for example that a textfile is locked by a wordprocess. </p> <p><strong>C#:</strong></p> <pre><code>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 &lt;= processes.GetUpperBound(0) - 1; i++) { myProcess = processes(i); if (!myProcess.HasExited) { try { ProcessModuleCollection modules = myProcess.Modules; int j = 0; for (j = 0; j &lt;= 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 : " &amp; exception.Message)) } } return myProcessArray; } } </code></pre> <p>VB.Net: </p> <pre><code>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 : " &amp; exception.Message)) End Try End If Next i Return myProcessArray End Function End Module </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload