我希望有人可以解释为什么Directory.GetCurrentDirectory()根据我如何将我的命令行参数传递给应用程序返回不同的结果(使用args下面的输出是如图所示Directory.GetCurrentDirectory()根据命令行参数返回不同的结果
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("The current directory is {0}", Directory.GetCurrentDirectory());
if(args != null && args.Any())
Console.WriteLine("Command line arguments are {0}", String.Join(", ", args));
Console.ReadLine();
}
}
如果你建立和使用命令提示符运行以下命令:VS在APP.EXE)拖动一个文件夹
直接进入它认为这一段代码你期望什么。它将输出的应用程序驻留在当前目录中。
C:\Projects\ArgumentTest\ApplicationA\bin\Debug\ApplicationA.exe C:\mydirectory
The current directory is C:\Projects\ArgumentTest\ApplicationA\bin\Debug\
Command line arguments are C:\mydirectory
如果你建立并通过拖动文件或文件夹在你得到不同的结果应用运行此程序。而不是返回预期的结果,而不是Directory.GetCurrentDirectory()返回您拖过应用程序的第一个文件的路径。
我目前得到了解决这个问题的方法,但是我很想理解为什么会发生这种情况。
其他信息:
- .NET 4.5
- 的Windows 2012R2(虚拟机)的机器
上
+1。如果需要的行为是获取EXE的包含文件夹,可能会有用处,可能会提到使用Process.GetCurrentProcess()。MainModule.FileName或Assembly.GetExecutingAssembly()。Location来获取该信息。 –
谢谢马克。我想我没有完全理解GetDirectory(),因为我从未在我的运行应用程序之外真正调用它。 – Svenkle