This question already has answers here:
How to start a process in the same folder as its executable

(3 个回答)


7年前关闭。




我有一个 c# 应用程序 A,它启动另一个 c# 应用程序 B,如下所示:
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string path = baseDir + "Programs\\Logging\\";
Process logger = new Process();
logger.StartInfo.FileName = System.IO.Path.Combine(path, "Logger.exe");
logger.Start();

在 Logger.exe 中,我执行以下操作:
string dir = Directory.GetCurrentDirectory();

但它告诉我 dir 是启动它的原始程序 A 的目录,而不是它自己的目录 (Programs\Logging)

为什么是这样??

最佳答案

那是正确的目录。它是您从中启动该进程的工作目录。如果要更改它,请执行以下操作:

string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string path = baseDir + "Programs\\Logging\\";
Process logger = new Process();
// Here's the deal
logger.StartInfo.WorkingDirectory = path;
logger.StartInfo.FileName = System.IO.Path.Combine(path, "Logger.exe");
logger.Start();

关于c# - Directory.GetCurrentDirectory();报告第二个 exe 的错误路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24312232/

10-11 15:52