为什么我不能在 C# 中使用 block 内部使用 Process.Start 方法(字符串,字符串)



我有一个带有此代码块的旧程序:

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

我想用using块优化代码,但我不能声明Process.Start Method (String, String)。

我试过这个:

    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
        }
    }

我的程序有什么问题?

你在 using block 中使用的启动方法是静态的。

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

你必须像

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