Defining a working directory for executing a program (C#)

后端 未结 2 1493
渐次进展
渐次进展 2020-12-11 17:40

I am currently trying to get an executable to be launched from a specific folder.

The code I have below crashes the application oddly enough:

Process         


        
2条回答
  •  遥遥无期
    2020-12-11 18:05

    The working directory that you set ("dump") is relative to the current working directory. You might want to check the current working directory.

    You should be able to set the working directory to the executing assemblies directory with this code...

    string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    Directory.SetCurrentDirectory(exeDir);
    

    Or, better yet, don't use a relative path, set p.StartInfo.WorkingDirectory to the absolute path.

提交回复
热议问题