public static Win32_Process[] GetProcessesWithOpenedFile(string machineName, string filename) { filename = filename.Replace(@"\", @"\\"); System.Management.ManagementObjectCollection mog = WmiAccess.GetInstancesByNameForFiles(machineName, "CIM_DataFile", filename); List<Win32_Process> lstProc = new List<Win32_Process>(); List<CIM_DataFile> _find_files = new List<CIM_DataFile>(); foreach (System.Management.ManagementObject o_file in mog) { CIM_DataFile f_o = new CIM_DataFile(o_file); _find_files.Add(f_o); } List<System.Threading.ManualResetEvent> _resets = new List<System.Threading.ManualResetEvent>(); Win32_Process[] procs = Win32_Process.GetWin32_Processes(machineName); int cnt = 0; foreach (Win32_Process prc in procs) { //CIM_DataFile[] files = prc.GetOpenedCIM_DataFiles(); //foreach (CIM_DataFile file in files) //{ // _search_file = file; // CIM_DataFile d_finded = _find_files.Find(find_file); // if (d_finded != null) // { // lstProc.Add(prc); // } //} _find_file_handler dlg = new _find_file_handler(_find_file); System.Threading.ManualResetEvent rs = new System.Threading.ManualResetEvent(false); dlg.BeginInvoke(_find_files, rs, prc, ref lstProc, null, null); _resets.Add(rs); cnt++; if (cnt == 60) { System.Threading.ManualResetEvent[] m_resets = _resets.ToArray(); System.Threading.WaitHandle.WaitAll(m_resets); _resets = new List<System.Threading.ManualResetEvent>(); cnt = 0; } } System.Threading.ManualResetEvent[] resets = _resets.ToArray(); System.Threading.WaitHandle.WaitAll(resets, new TimeSpan(0,5,0)); System.Threading.Thread.CurrentThread.Join(500); //foreach (System.Management.ManagementObject mo in mog) //{ // System.Management.ManagementObjectCollection mg_C = mo.GetRelated(); // foreach (System.Management.ManagementObject mo_L in mg_C) // { // Console.WriteLine(mo_L.ClassPath); // System.Management.ManagementObjectCollection mg_CC = mo_L.GetRelated("Win32_Process"); // foreach (System.Management.ManagementObject mo_LL in mg_CC) // { // Console.WriteLine(mo_LL.ClassPath); // } // //Win32_Process prc = new Win32_Process(mo_L); // //lstProc.Add(prc); // } //} return lstProc.ToArray(); }
public static Win32_Process[] GetProcessesWithOpenedFile(string machineName, string filename) { filename = filename.Replace(@"\", @"\\"); System.Management.ManagementObjectCollection mog = WmiAccess.GetInstancesByNameForFiles(machineName, "CIM_DataFile", filename); List <Win32_Process> lstProc = new List <Win32_Process>(); List <CIM_DataFile> _find_files = new List <CIM_DataFile>(); foreach (System.Management.ManagementObject o_file in mog) { CIM_DataFile f_o = new CIM_DataFile(o_file); _find_files.Add(f_o); } List <System.Threading.ManualResetEvent> _resets = new List <System.Threading.ManualResetEvent>(); Win32_Process[] procs = Win32_Process.GetWin32_Processes(machineName); int cnt = 0; foreach (Win32_Process prc in procs) { //CIM_DataFile[] files = prc.GetOpenedCIM_DataFiles(); //foreach (CIM_DataFile file in files) //{ // _search_file = file; // CIM_DataFile d_finded = _find_files.Find(find_file); // if (d_finded != null) // { // lstProc.Add(prc); // } //} _find_file_handler dlg = new _find_file_handler(_find_file); System.Threading.ManualResetEvent rs = new System.Threading.ManualResetEvent(false); dlg.BeginInvoke(_find_files, rs, prc, ref lstProc, null, null); _resets.Add(rs); cnt++; if (cnt == 60) { System.Threading.ManualResetEvent[] m_resets = _resets.ToArray(); System.Threading.WaitHandle.WaitAll(m_resets); _resets = new List <System.Threading.ManualResetEvent>(); cnt = 0; } } System.Threading.ManualResetEvent[] resets = _resets.ToArray(); System.Threading.WaitHandle.WaitAll(resets, new TimeSpan(0, 5, 0)); System.Threading.Thread.CurrentThread.Join(500); //foreach (System.Management.ManagementObject mo in mog) //{ // System.Management.ManagementObjectCollection mg_C = mo.GetRelated(); // foreach (System.Management.ManagementObject mo_L in mg_C) // { // Console.WriteLine(mo_L.ClassPath); // System.Management.ManagementObjectCollection mg_CC = mo_L.GetRelated("Win32_Process"); // foreach (System.Management.ManagementObject mo_LL in mg_CC) // { // Console.WriteLine(mo_LL.ClassPath); // } // //Win32_Process prc = new Win32_Process(mo_L); // //lstProc.Add(prc); // } //} return(lstProc.ToArray()); }