Search code examples
cmdworking-directory

how to run a program from cmd when trying to execute from path throws errors


I am trying to write a C# program that executes a .exe application. In the cmd prompt if I type the full directory (C:\Users\User\Program\program.exe) I get thrown an error:

Error message: Can't open config file data/program.cfg.
Error reading data/program.cfg

However if I Cd to C:\Users\User\Program\ I can start the program by typing program.exe or program, or alternatively by just double-clicking the application file. I also tried adding the directory to my PATH but it throws the same error.

What could be causing this issue?


Solution

  • The issue is that the program has a bug because of which it looks for its config file (data\program.cfg) relative to the current directory, rather than relative to the directory where it is installed.

    You can work around this in your .NET program by using System.Diagnostics.Process.Start with a ProcessStartInfo parameter, where you will set WorkingDirectory to the directory C:\Users\User\Program.