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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With