How to code running an application with switches without calling cmd

Soar 116 Reputation points
2022-07-09T05:36:09.333+00:00

I wrote a simple Windows form app to run Disk clean-up with the following code:

private void button1_Click(object sender, EventArgs e)
{
string strCmdText;
strCmdText = "/c cleanmgr /d c";
Process.Start("cmd.exe", strCmdText);
}

And it worked like a charm.

However, I thought why don't I just run my command without cmd, like when we type executables in the Run command such as mspaint, winword, msconfig, etc. So I made it like this instead:

private void button1_Click(object sender, EventArgs e)
{
Process.Start("/c cleanmgr /d c");
}

I also tried "cleanmgr /d c", but when I ran the app and clicked on that button, I received "Unhandled exception has occurred in your application...". But when I just used "cleanmgr", it worked.

What is the best and shortest way to run my command without cmd? And in terms of best practices or performance, do you think running it with cmd is better than running it without it?

Developer technologies | Windows Forms
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2022-07-09T06:43:58.013+00:00

    For example :

               using (Process p = new Process())  
                {  
                    p.StartInfo.FileName = "cleanmgr";  
                    p.StartInfo.Arguments = "/d c";  
                    p.Start();  
                }
    

  2. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2022-07-09T18:10:42.717+00:00

    You can start a process and pass arguments using either of the following options:

    1. Use one of the static overloads of the Start method (for example this one ) and pass the appropriate parameters
    2. Instantiate a new Process, set the appropriate members of the StartInfo property and call Start()

    In your command cleanmgr /d c , the executable is cleanmgr and arguments are /d c. So you can run it like this:

    var p = System.Diagnostics.Process.Start("cleanmgr", "/d c");  
    

    Or this:

    var p = new System.Diagnostics.Process();  
    p.StartInfo.FileName = "cleanmgr";  
    p.StartInfo.Arguments = "/d c";  
    p.Start();  
    

    As mentioned in the documentations, the type is disposable and should be disposed as soon as you are done with it. You can dispose it by calling its Dsipose() method in a try/finally block or use it in a using block, for example:

    var p = new System.Diagnostics.Process();  
    try  
    {  
        p.StartInfo.FileName = "cleanmgr";  
        p.StartInfo.Arguments = "/d c";  
        p.Start();  
    }  
    finally  
    {  
        p.Dispose();  
    }  
    

    Or

    using (var p = new System.Diagnostics.Process())  
    {  
        p.StartInfo.FileName = "cleanmgr";  
        p.StartInfo.Arguments = "/d c";  
        p.Start();  
    }  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.