Jacob W
Jacob W

Reputation: 31

File directory from string

I have a little problem with my C# code. Concept: after choosing file from FileDialog, I want to write file's directory to string and later - basing on this string - I want to run file from the path, but instead I have error 'The path is not of a legal form'. Does anyone know how to fix it?

    private void button2_Click_2(object sender, EventArgs e)
    {
        var FD = new System.Windows.Forms.OpenFileDialog();
        if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string fileToOpen = FD.FileName;

            System.IO.FileInfo File = new System.IO.FileInfo(FD.FileName);
            label8.Text = fileToOpen;

            string sciezka = label8.Text;
            label9.Text = sciezka; 
        }
    }

    private void button3_Click_1(object sender, EventArgs e)
    {
        if (radioButton6.Checked == false)
        {
            MessageBox.Show("Proszę wybrać opcję 'Sterowanie klawiaturą.", "Uwaga!",
            MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else if (label8.Text == "")
        {
            MessageBox.Show("Proszę wybrać plik do sterowania robotem.", "Uwaga!",
            MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        else
        {
            var startInfo = new ProcessStartInfo();

            startInfo.WorkingDirectory = Path.GetFullPath(sciezka);

            Process proc = Process.Start(startInfo);
        }
    }

Upvotes: 2

Views: 86

Answers (2)

Mike Hoare
Mike Hoare

Reputation: 1

startInfo.WorkingDirectory = Path.GetFullPath(sciezka);
startInfo.FileName = sciezka;
Process proc = Process.Start(startInfo);

Upvotes: -1

Owen Pauling
Owen Pauling

Reputation: 11871

sciezka is your filename and you are getting the full path to it with:

Path.GetFullPath(sciezka)

However, startInfo.WorkingDirectory expects a directory, not a path to a file.

ProcessStartInfo.WorkingDirectory

When the UseShellExecute property is false, gets or sets the working directory for the process to be started. When UseShellExecute is true, gets or sets the directory that contains the process to be started.

Upvotes: 4

Related Questions