Question

I have a batch file I am running with Process.Start() in C#. Here is my code:

Process proc = new Process();

proc.StartInfo.FileName = cmd;
proc.StartInfo.Arguments = args;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = true;

proc.Start();

proc.WaitForExit();

return proc.ExitCode;

When I open the command line and type echo %ProgramFiles% it returns C:\Program Files. Inside of the batch file (which is being run by the C# proc.Start() call), it expands %ProgramFiles% and it equals C:\Program Files (x86). I can't figure out why this is happening and it's breaking my code. How can I fix it and why is it happening?

Was it helpful?

Solution

Most likely it is because your C# code is being compiled as x86 and Windows wants to keep backwards compatibility so Program Files points to the x86 version.

Compile as 64-bit and it will point to the 64-bit Program Files folder.

Now if you want your program to run on x86 versions of Windows you will need to compile as x86 and add some manual handling for this situation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top