Search code examples
c#processusing

Why I cannot use the Process.Start Method (String,String) inside using block in C#?


I have an old program with this code block:

private void openConfigToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (!File.Exists(Path.Combine(a, b))) { writeConf(); }
    Process.Start("notepad.exe", Path.Combine(c, d));

}

I would like to optimize the code with using block, but I cannot declare Process.Start Method (String, String).

I tried this:

    private void openConfigToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (!File.Exists(Path.Combine(a, b))) { writeConf(); }

        using (Process proc = new Process())
        {
            proc.Start("notepad.exe", Path.Combine(c, d)); //Problem
        }

    }

What is the problem with my program?


Solution

  • The start method you used inside using block is static.

    public static Process Start(string fileName, string arguments);
    

    You have to call like

    using (Process proc = new Process())
    {
        proc.StartInfo.Arguments = Path.Combine(c, d);
        proc.StartInfo.FileName = "notepad.exe";
        proc.Start();
    }