Directory.GetCurrentDirectory()不能在linux上运行?

时间:2012-10-16 13:49:56

标签: c# linux mono

所以我正在尝试创建一个需要在名为“scripts”的子文件夹中读取脚本的应用程序。我的代码我遇到了问题:

string script = Console.ReadLine();
string path = Directory.GetCurrentDirectory();
string sciptpath  = path + "/scripts/" + script;

这在Windows上运行正常。但是在Linux上(使用Mono Runtime运行)它会转到当前用户的主目录...而不是可执行文件的目录。这是一个错误吗?有人可以建议解决方法吗?

1 个答案:

答案 0 :(得分:11)

并不是它需要“修复”它是当前目录不是你想象的那样。当前目录是相对路径“具有焦点”的目录。无论您的EXE在哪里,您当前的目录都可以在其他任何地方,或者甚至可能在执行期间更改。

你想要的是:

string path = Path.GetDirectoryName(Application.ExecutablePath);