Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.Start can't find an existing file

  • I have the path of an executable file (C:\Test\n4.TestConsole.exe).
  • File.Exists(path) returns true.
  • File.OpenRead(path) gets me its stream with no problem.
  • Process.Start(path) throws a System.ComponentModel.Win32Exception with this message:

    The system cannot find the file specified.

What am I doing wrong?

Windows 8 Professional x64 - .NET Framework 4.5


Edit: Here is the code.

public partial class Form1 : Form
{
    public string Path { get; set; }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // I put a breakpoint here and verify the Path's value is
        // C:\Test\n4.TestConsole.exe.

        // File.Exists returns true.
        MessageBox.Show(File.Exists(Path));

        // File.OpenRead doesn't throw an exception.
        using (var stream = File.OpenRead(Path)) { }

        // This throws the exception.
        Process.Start(Path);
    }
}
like image 623
Şafak Gür Avatar asked Oct 22 '22 02:10

Şafak Gür


2 Answers

It's probably a missing DLL or other dependency. You might like to compare the PATH environment variable when you run it directly via Process.Start(exe_path) and when you run it via Process.Start("cmd", "/k " + exe_path).

like image 177
Rich Tebb Avatar answered Oct 27 '22 11:10

Rich Tebb


Try this:

private void button1_Click(object sender, EventArgs e)
{
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.WorkingDirectory = @"C:\Test";
    psi.FileName = "n4.TestConsole.exe";
    Process.Start(psi);
}
like image 25
t3hn00b Avatar answered Oct 27 '22 10:10

t3hn00b