Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. /// <summary>
  2. /// Return the first process id with matching values in the command line.
  3. /// </summary>
  4. /// <param name="args">
  5. /// Values in the command line to match (case insensitive).
  6. /// </param>
  7. /// <returns>
  8. /// The process id of the first matching process found; null on no match.
  9. /// </returns>
  10. public static int? ProcessIdOf(params string[] args)
  11. {
  12. using (var mos = new ManagementObjectSearcher(
  13. "SELECT ProcessId,CommandLine FROM Win32_Process"))
  14. {
  15. foreach (ManagementObject mo in mos.Get())
  16. {
  17. var commandLine = (string)mo["CommandLine"] ?? string.Empty;
  18. for (int i = 0;; ++i)
  19. {
  20. if (i == args.Length)
  21. {
  22. return int.Parse(mo["ProcessId"].ToString());
  23. }
  24.  
  25. if (commandLine.IndexOf(
  26. args[i],
  27. StringComparison.InvariantCultureIgnoreCase) == -1)
  28. {
  29. break;
  30. }
  31. }
  32. }
  33. }
  34.  
  35. return null;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement