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

本文关键字:exe 错误 路径 第二个 报告 GetCurrentDirectory Directory | 更新日期: 2023-09-27 18:09:17

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

为什么??

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

这是正确的目录。这是启动该进程的工作目录。如果你想改变它,像这样做:

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

Per 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

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