Search code examples
c#processsystem.diagnostics

Unable to set Process.StartInfo.WorkingDirectory to call exe from c#


I'm trying to call chrome.exe inside a C# program by using System.Diagnostics.Process namespace.

my chrome.exe is located inside path C:\Program Files (x86)\Google\Chrome\Application

if I call RunProc function by passing bellow parameters - (keep absolute path of the exe and keep WorkingDirectory empty)

("C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe" , "https://www.google.com", "") it works just fine.

But, with parameters -

("Chrome.exe , "https://www.google.com", "C:\Program Files (x86)\Google\Chrome\Application") it gives exception at step proc.Start(); stating - The system cannot find the file specified.

I also tried writing WorkingDirectory = workingDir while initializing StartInfo but still looking for solutions.

class Program
{
    static void Main(string[] args)
    {
        RunProc(@"chrome.exe", @"https://www.google.com", @"C:\Program Files (x86)\Google\Chrome\Application");
    }

    static bool RunProc(string exe, string args, string workingDir)
    {
        Process proc = new Process
        {
            StartInfo =
            {
                FileName =  exe,
                CreateNoWindow = true,
                RedirectStandardInput = true,
                WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                Arguments = args,
                //WorkingDirectory = workingDir
            }
        };

        if (!string.IsNullOrEmpty(workingDir))
        {
            proc.StartInfo.WorkingDirectory = workingDir;
        }

        proc.Start();
        proc.StandardInput.WriteLine(args);
        proc.StandardInput.Flush();
        proc.StandardInput.Close();

        return true;
    }

}

Solution

  • The only way for this to work is for you to change your working directory to the passed in working directory before attempting to start the other process. The WorkingDirectory property is just that, and doesn't in any way get involved in locating the executable to run. That just relies on your working directory and your PATH environment variable, if you fail to provide a fully-qualified name.

    static bool RunProc(string exe, string args, string workingDir)
    {
        var prevWorking = Environment.CurrentDirectory;
        try
        {
            Environment.CurrentDirectory = workingDir;
            Process proc = new Process
            {
                StartInfo =
                {
                   FileName =  exe,
                   CreateNoWindow = true,
                   RedirectStandardInput = true,
                   WindowStyle = ProcessWindowStyle.Hidden,
                   UseShellExecute = false,
                   RedirectStandardError = true,
                   RedirectStandardOutput = true,
                   Arguments = args,
                }
            };
    
            proc.Start();
            proc.StandardInput.WriteLine(args);
            proc.StandardInput.Flush();
            proc.StandardInput.Close();
    
            return true;
        }
        finally
        {
            Environment.CurrentDirectory = prevWorking;
        }
    }