如何获取运行程序的控制台的当前工作目录,以便我可以解析作为程序参数传递的相对路径?
假设我把我的程序放在这里:c:\tools\program.exe
但我从不同的地方调用它。假设我在这里:C:\Users\Me\Documents\我运行这个命令program.exe --src=somefile.txt --dest=subdir\otherfile.txt
Environment.CurrentDirectory并且System.Reflection.Assembly.GetExecutingAssembly().Location会返回c:\tools,但我希望能够解析somefile.txt与.subdir\oterfile.txtC:\Users\Me\Documents\
====== 更新 ======
谢谢您的帮助。似乎
Environment.CurrentDirectory按预期工作。事实证明,在我的情况下,问题是由 Xenocode 的 Postbuild 工具(现在称为 Spoon Virtual Application Studio)引起的,我偶尔会使用该工具将所有程序的文件(包括 dll、配置等)“打包”到一个可执行文件中。它非常方便,但在这种情况下,“虚拟化”功能会弄乱我程序的Environment变量。我已经设法解决了这个问题。