Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory.GetCurrentDirectory() not working on linux?

Tags:

c#

linux

mono

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?

like image 349
user1750201 Avatar asked Oct 16 '12 13:10

user1750201


1 Answers

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);
like image 148
Corey Ogburn Avatar answered Nov 10 '22 12:11

Corey Ogburn