Directory.GetCurrentDirectory() not working on linux?

拜拜、爱过 提交于 2021-02-07 12:32:06

问题


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;

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?


回答1:


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.

What you want is:

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


来源:https://stackoverflow.com/questions/12916204/directory-getcurrentdirectory-not-working-on-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!