Search code examples
c#processopenfiledialog

Take user input and use it as Process


So basically, I have a OpenFileDialog where the user will select a location. I made it so it would display the directory in a textbox. But what I want is have another button which would take that directory and start it by using ProcessStartInfo.

OpenFileDialog, showing it in 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;
        }
    }

Process:

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

Solution

  • Since you already save the result of the OpenFileDialog in textBox1, you can easily access it in the button3_Click event handler.

    To fill the startInfo.FileName:

    *I added an extra IsNullOrWhiteSpace check, so the application doesn't start another process if the textBox1.Text is empty.

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