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

问题描述 投票:1回答:2

这个问题在这里已有答案:

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

为什么是这样??

c# .net
2个回答
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
投票

根据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

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

© www.soinside.com 2019 - 2024. All rights reserved.