Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Directory.GetCurrentDirectory()在linux上不工作?_C#_Linux_Mono - Fatal编程技术网

C# Directory.GetCurrentDirectory()在linux上不工作?

C# Directory.GetCurrentDirectory()在linux上不工作?,c#,linux,mono,C#,Linux,Mono,所以我试图创建一个应用程序,它需要读取名为“scripts”的子文件夹中的脚本。我的代码有问题: string script = Console.ReadLine(); string path = Directory.GetCurrentDirectory(); string sciptpath = path + "/scripts/" + script; 这在Windows上运行良好。但是在Linux(使用Mono运行时运行)上,它会转到当前用户的主目录…而不是可执行文件的目录。这是虫子吗

所以我试图创建一个应用程序,它需要读取名为“scripts”的子文件夹中的脚本。我的代码有问题:

string script = Console.ReadLine();
string path = Directory.GetCurrentDirectory();
string sciptpath  = path + "/scripts/" + script;
这在Windows上运行良好。但是在Linux(使用Mono运行时运行)上,它会转到当前用户的主目录…而不是可执行文件的目录。这是虫子吗?有人能提出一个解决方案吗?

不是需要“修复”,而是当前目录不是您所认为的那样。当前目录是相对路径“具有焦点”的目录。无论EXE在何处,当前目录都可以位于其他任何位置,甚至可能在执行过程中发生更改

你想要的是:

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

你检查过单声道兼容性了吗?您还可以尝试Mono迁移分析器(MoMA)工具,该工具有助于识别将.Net应用程序移植到Mono()时可能遇到的问题。这不是您的问题,但不要连接路径,而是使用Path.Combine:string scriptpath=Path.Combine(Path,“scripts”,script);-它可以在windows和linux上正常工作。谢谢,这很好。实际上我只需要做Directory.SetCurrentDirectory(Path.GetDirectoryName(Application.ExecutablePath));来解决这个问题。非常感谢你!除非有充分的理由,否则不要修改当前目录。一般来说,您应该将当前目录视为由用户控制的资源,并且在处理用户发起的请求时(例如,从GUI应用程序中的某个对话框打开文件)应该使用和/或修改当前目录。您应该仅在用户需要时修改它。当您自动加载作为应用程序一部分的其他文件时,用户通常看不到这些文件,或者至少用户不应该为此烦恼。因此,根据应用程序的安装目录计算路径,而不使用当前目录;var scriptdir=Path.Combine(路径,“脚本”);var script=Path.Combine(scriptdir,script)@MartinBaulig,没错。也许用var,它们只是字符串。