Google
 

Saturday, March 31, 2007

Use System.Diagnostics.Process in C#

  1. The Process class in  the System.Diagnostics name space
  2. // Static Start with no reference to Process Object
                Process.Start( "Notepad");

  3. //  Search for the Notepad process just created
                Process[] processArray =  Process.GetProcessesByName("notepad");

                
    if (processArray[0].ProcessName  == "notepad" )
                    Console.WriteLine(
    "Notepad started ");
                
    else
                    Console.WriteLine(
    "Notepad either reused or not started");


  4. //  Static Start with reference to Process Object 
                Process calcApp;
                calcApp 
    = Process.Start("Calc" );
                
    if (calcApp !=  null
                    Console.WriteLine(
    "Calc started ");
                
    else
                    Console.WriteLine(
    "Calc either reused or not started");

  5.      // Static Start with reference to Process Object
                Process IEApp  = Process.Start("iexplore" );
                
    if (IEApp !=  null
                    Console.WriteLine(
    "IE started ");
                
    else
                    Console.WriteLine(
    "IE either reused or not started");

  6. //  Instance Start with StartInfo
                Process wordApp =  new Process();
                wordApp.StartInfo.FileName 
    =  "winword";
                
    bool wordStarted = wordApp.Start();
                
    if (wordStarted) 
                    Console.WriteLine(
    "Word started ");
                
    else
                    Console.WriteLine(
    "Word either reused or not started");

  7. //  Static Start with Document
                Process IEApp2 =  Process.Start("http://www.google.com" );
                
    if (IEApp2 ==  null)
                    Console.WriteLine(
    "Second instance of IE started ");
                
    else
                    Console.WriteLine(
    "IE instance reused or not started");

  8. //  Instance Start with Document as StartInfo
                Process IEApp3 =  new Process();
                IEApp3.StartInfo.FileName 
    =  "http://www.microsoft.com" ;
                
    bool IEstarted = IEApp3.Start();
                
    if (IEstarted)
                    Console.WriteLine(
    " IE instance started");
                
    else
                    Console.WriteLine(
    "IE instance either reused or not started" );


--
Happy day, happy life!

No comments: