简体   繁体   English

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

[英]Directory.GetCurrentDirectory() not working on linux?

So I'm trying to create an application that requires the reading of scripts in a sub-folder called "scripts". 所以我正在尝试创建一个需要在名为“scripts”的子文件夹中读取脚本的应用程序。 My code I'm having issues with: 我的代码我遇到了问题:

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

This works fine on Windows. 这在Windows上运行正常。 But on Linux (running using Mono Runtime) it goes to the current users home Directory...not the directory of the executable. 但是在Linux上(使用Mono Runtime运行)它会转到当前用户的主目录...而不是可执行文件的目录。 Is this a bug? 这是一个错误吗? And can someone suggest a workaround? 有人可以建议一个解决方法吗?

It's not that it needs "fixing" it's that the current directory is not what you think it is. 并不是它需要“修复”它是当前目录不是你想象的那样。 The current directory is the directory that "has focus" for relative paths. 当前目录是相对路径“具有焦点”的目录。 Regardless where your EXE is, your current directory can be anywhere else, or may even change during execution. 无论您的EXE在哪里,您当前的目录都可以在其他任何地方,或者甚至可能在执行期间更改。

What you want is: 你想要的是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Directory.GetCurrentDirectory - Directory.GetCurrentDirectory 将转义序列添加到Directory.GetCurrentDirectory() - adding escape sequence to Directory.GetCurrentDirectory() WPF设置中的默认值为“ Directory.GetCurrentDirectory”。 - Default value in WPF Settings as `Directory.GetCurrentDirectory` 无法将文件写入 Directory.GetCurrentDirectory() 中的路径 - Unable to write file to path in Directory.GetCurrentDirectory() C#Directory.GetCurrentDirectory()返回带有NUnit的system32 - C# Directory.GetCurrentDirectory() returning system32 with NUnit Directory.GetCurrentDirectory()根据命令行参数返回不同的结果 - Directory.GetCurrentDirectory() returns different results based on command line arguments C#:从浏览器启动时的Directory.getCurrentDirectory() - C#: Directory.getCurrentDirectory() when launching from Browser Directory.GetCurrentDirectory 在单元测试中抛出 System.IO.DirectoryNotFoundException - Directory.GetCurrentDirectory Throwing System.IO.DirectoryNotFoundException in unit tests Environment.CurrentDirectory 和 Directory.GetCurrentDirectory 有什么区别? - What is the difference between Environment.CurrentDirectory and Directory.GetCurrentDirectory? C#Directory.GetCurrentDirectory和C以外的驱动器: - C# Directory.GetCurrentDirectory and drives other than C:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM