2019-11-29-dotnet-通过-WMI-获取指定进程的输入命令行
原文:2019-11-29-dotnet-通过-WMI-获取指定进程的输入命令行
title | author | date | CreateTime | categories |
---|---|---|---|---|
dotnet 通过 WMI 获取指定进程的输入命令行 | lindexi | 2019-11-29 08:35:39 +0800 | 2019-02-22 10:17:37 +0800 | dotnet C# WMI |
本文告诉大家如何使用 WMI 通过 Process 获取这个进程传入的命令行
使用下面代码,使用 Win32_Process 拿到所有的进程,通过 WHERE 判断当前的进程,然后拿到进程传入的命令
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(); } }
获取所有的进程的命令行参数
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. } } }
如果不能访问 WMI 如使用的是 dotnet core 2.0 以下版本或需要通过 dotnet core 编译为 Native 就可以尝试不使用 WMI 在 dotnet 获取指定进程的输入命令行
https://stackoverflow.com/a/2633674/6116637
更多 WMI 请看 WMI 博客
相关推荐
吹云 2020-11-05
84407805 2020-11-01
曾是土木人 2020-10-31
87201442 2020-10-15
java0 2020-09-29
81437716 2020-09-18
81437716 2020-09-13
bluecarrot 2020-09-11
myCat 2020-09-09
83427718 2020-09-07
85560694 2020-09-03
89520991 2020-08-21
89550191 2020-08-09
katie000 2020-08-04
hunningtu 2020-07-30
89961330 2020-07-28
yuzhongdelei0 2020-07-28
yuzhongdelei0 2020-07-27