问题
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