这正在使用所有托管对象,但确实涉及到WMI领域:
private static void Main(){ foreach (var process in Process.GetProcesses()) { try { Console.WriteLine(process.GetCommandLine()); } catch (Win32Exception ex) when ((uint)ex.ErrorCode == 0x80004005) { // Intentionally empty - no security access to the process. } catch (InvalidOperationException) { // Intentionally empty - the process exited before getting details. } }}private static string GetCommandLine(this Process process){ using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECt CommandLine FROM Win32_Process WHERe ProcessId = " + process.Id)) using (ManagementObjectCollection objects = searcher.Get()) { return objects.Cast<ManagementbaseObject>().SingleOrDefault()?["CommandLine"]?.ToString(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)