Directory.GetCurrentDirectory()在Linux上不起作用? [英] Directory.GetCurrentDirectory() not working on linux?

查看:163
本文介绍了Directory.GetCurrentDirectory()在Linux上不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图创建一个需要在名为"scripts"的子文件夹中读取脚本的应用程序.我的代码遇到以下问题:

So I'm trying to create an application that requires the reading of scripts in a sub-folder called "scripts". My code I'm having issues with:

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

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

This works fine on Windows. But on Linux (running using Mono Runtime) it goes to the current users home Directory...not the directory of the executable. Is this a bug? And can someone suggest a workaround?

推荐答案

不是需要修复",而是当前目录不是您想的那样.当前目录是相对路径具有焦点"的目录.无论您的EXE在哪里,您的当前目录都可以在其他任何位置,甚至在执行过程中可能会更改.

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.

您想要的是:

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

这篇关于Directory.GetCurrentDirectory()在Linux上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆