[C#] System.Diagnostics.Process

Discussion in 'Mixed Languages' started by S_A_S, Dec 5, 2012.

  1. S_A_S

    S_A_S MDL Member

    Oct 16, 2012
    236
    103
    10
    Hi,

    Hope that somebody can enlighten me...

    What's the difference between System.Diagnostics.Process and System.Diagnostics.ProcessStartInfo ?

    I've been browsing the www the last couple of day, trying to understand how C# works, but can't see the trees through the forest ;)

    Noticed though that .Process is mainly used to call .vbs, and .ProcessStartInfo for CMD.

    Regards.
     
  2. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,171
    120
    Two different classes for working with processes.

    In a first moment, ProcessStartInfo is enrolled with the Process class.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. S_A_S

    S_A_S MDL Member

    Oct 16, 2012
    236
    103
    10
    #3 S_A_S, Dec 5, 2012
    Last edited: Dec 5, 2012
    (OP)
    So which is the correct one to use for calling cmd.exe or slmgr.vbs?

    Also noticed differences in the line when it's used.

    example:

    1)
    System.Diagnostics.Process Port = new System.Diagnostics.Process();
    Port.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
    Port.StartInfo.Arguments = "/c netstat -a";
    Port.Start();

    2)
    System.Diagnostics.ProcessStartInfo Port = new System.Diagnostics.ProcessStartInfo("cmd", "/c netstat -a");

    Both doing the same thing.
     
  4. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,460
    420
    60
    ok you know that Port.Startinfo is just the ProcessStartInfo class \ struct

    you can create a process with it like this

    Port.Start(ProcStartInfo);

    or do it the way you did..

    in the post above :D
     
  5. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,171
    120
    #5 Josh Cell, Dec 5, 2012
    Last edited by a moderator: Apr 20, 2017
    ProcessStartInfo is one enrolled class. It only will work as dependant for the Process class, such as:

    Code:
    ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe"); //Initializing the startInfo object.
    startInfo.WindowStyle = ProcessWindowStyle.Minimized; //Adding the WindowStyle property.
    startInfo.Arguments = "http://forums.mydigitallife.net"; //Adding the Arguments property.
    Process.Start(startInfo); //Starting a new process using the startInfo properties...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. S_A_S

    S_A_S MDL Member

    Oct 16, 2012
    236
    103
    10
    #6 S_A_S, Dec 6, 2012
    Last edited: Dec 6, 2012
    (OP)
    VS doesnt let me take those shortcuts (eg ProcessStartInfo startInfo = new ProcessStartInfo) in WFA, it wants me to always append System.Diagnostics.

    But i guess that i'm on the right track, just need to do some more digging...

    By the way, how can i run a elevated command, "runas" doesnt work, trows me a "OneClick app can not run as administrator" error, and from what i find on the www is that MS implemeted that restriction.
     
  7. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    #7 Calistoga, Dec 6, 2012
    Last edited by a moderator: Apr 20, 2017
    Add this to the top of your code file:
    Code:
    using System.Diagnostics;
     
  8. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,171
    120
    #8 Josh Cell, Dec 6, 2012
    Last edited by a moderator: Apr 20, 2017
    You need of run your main executable as administrator to automatically invoke other processes with administrator level.

    Here's the way to elevate it programatically:

    Code:
                    ProcessStartInfo proc = new ProcessStartInfo();
                    proc.UseShellExecute = true;
                    proc.WorkingDirectory = Environment.CurrentDirectory;
                    proc.FileName = Application.ExecutablePath;
                    proc.Verb = "runas";
     
                    try
                    {
                        Process.Start(proc);
                    }
                    catch
                    {
                        // The user refused the elevation.
                        // Do nothing and return directly ...
                        return;
                    }
                    Application.Exit();  // Quit itself
    Ah, if you're trying to execute shortcuts, you need of set this property:

    Code:
     proc.UseShellExecute = true;
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,460
    420
    60
    remove the publishing bindings from your program or uncheck the sign the click once manifests. sounds like you are using visual studio 2008 :p
     
  10. S_A_S

    S_A_S MDL Member

    Oct 16, 2012
    236
    103
    10
    VS Pro 2012
     
  11. Dermot

    Dermot MDL Novice

    Mar 14, 2011
    2
    0
    0
    #11 Dermot, Aug 29, 2013
    Last edited by a moderator: Apr 20, 2017
    Code:
    Process.Start("Runas"," /user:" + Environment.UserName + " whatever.exe");
    
    This will prompt for the admin password before executing the application as administrator

    Just another way to do it i suppose.