Directory.GetCurrentDirectory();报告第二个exe的错误路径

时间:2014-06-19 16:57:33

标签: c# .net

我有一个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)

为什么这???

2 个答案:

答案 0 :(得分:4)

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

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();

答案 1 :(得分:1)

每个MSDN,The current directory is distinct from the original directory, which is the one from which the process was started.

http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory(v=vs.110).aspx

所以,它正在做正确的事。