获取用户输入并将其用作Process

时间:2016-05-28 12:36:56

标签: c# process openfiledialog

基本上,我有一个OpenFileDialog,用户将选择一个位置。 我做了它,所以它会在文本框中显示目录。 但我想要的是另一个按钮,它将采用该目录并使用ProcessStartInfo启动它。

OpenFileDialog,在TextBox中显示:

    public void button4_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "Open Arma 3";
        ofd.Filter = "EXE file|*.exe";

        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            textBox1.Text = ofd.FileName;
        }
    }

过程:

    private void button3_Click(object sender, EventArgs e)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = //RESULT OPENFILEDIALOG SHOULD BE HERE 
        startInfo.Arguments = @"-window -useBE -mod=e:\Aaron\Addons\@CBA_A3";
        Process.Start(startInfo);
    }

1 个答案:

答案 0 :(得分:0)

由于您已经在textBox1中保存了OpenFileDialog的结果,因此您可以在button3_Click事件处理程序中轻松访问它。

填写startInfo.FileName:

*我添加了额外的IsNullOrWhiteSpace检查,因此如果textBox1.Text为空,应用程序不会启动另一个进程。

if(!string.IsNullOrWhiteSpace(textBox1.Text)
{
      ProcessStartInfo startInfo = new ProcessStartInfo();
      startInfo.FileName = textBox1.Text
      startInfo.Arguments = @"-window -useBE -mod=e:\Aaron\Addons\@CBA_A3";
      Process.Start(startInfo);
}
相关问题