2012年6月6日水曜日

How to get command line of process on C#.

I found how to get the command line of a process on C#.

This source would wait until inputting process(command line args) had stopped.

This source need adding System.Manegement.dll.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace WaitATask
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = args[0];
            //System.Diagnostics.Process ps = System.Diagnostics.Process.Start(@"c:\TestZZ.bat");
            //System.Threading.Thread.Sleep(3000);
            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("cmd"))
            {
                using (ManagementObjectSearcher mos = new ManagementObjectSearcher(
                    string.Format("SELECT CommandLine FROM Win32_Process WHERE ProcessId = {0}",p.Id)))
                {
                    foreach (ManagementObject mo in mos.Get())
                    {
                        string cmdLine=mo["CommandLine"].ToString();
                        if (cmdLine.ToUpper().Contains(name.ToUpper() )){
                            p.WaitForExit();
                        }
                    }
                }
            }
        }
    }
}

0 件のコメント:

コメントを投稿