2011年7月20日 星期三

[C#] Get the cmdline from a different process

We can use Win32_Process management object to get the command line. For example,
var query = 
"select CommandLine from Win32_Process where ...";

using (var searcher = new ManagementObjectSearcher(query)) {
foreach (var result in searcher.Get()) {
// The desired cmdline is (String)result["CommandLine"]
}
}
Before using Win32_Process, we should add System.Management.dll to the reference. Surely, we can wrap up the sample code in a function.

沒有留言:

張貼留言