0

I have a C# program that will be executed by mono. From the C# program, I want to get the path to the mono vm binary that is running the program. Is there an API for that?

Note that I'm not asking for the path to the C# program, but the path to the mono vm binary (e.g. /usr/local/bin/mono).

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Andy Li
  • 5,894
  • 6
  • 37
  • 47
  • For the downvote and close request: I'm not asking for a library, but a way to get the path to mono. It is similar to other popular questions like http://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in – Andy Li Jan 07 '16 at 14:38
  • Is not related to c# or even to mono, more to Linux itsef. command **type -a mono** gives you real path to mono, typed in shell, command **ps -ax | grep mono | grep hello** gives output for you helloworld.exe executed by mono (the same result you can achieve from object System.Diagnostics.Process, will be more windows-way ) – vitalygolub Jan 07 '16 at 17:16
  • What if I'm using mono on Windows? And the reason I'm considering this a C#/Mono question is that, there exist such APIs in other languages. e.g. in Python, there is [`sys.executable`](https://docs.python.org/2/library/sys.html#sys.executable). I'm looking for an equivalent of that. – Andy Li Jan 07 '16 at 17:38
  • Process.GetProcessByName() is crossplatform. Am not sure about behavior of Process.StartInfo, but at least it is present – vitalygolub Jan 07 '16 at 18:34

1 Answers1

2

You could do what MonoDevelop does to find the location of Mono. This is based on finding the location of an assembly and then going back up four directories to find the prefix where Mono is currently being used from.

The location of the assembly containing int is first found.

string location = typeof (int).Assembly.Location;

Then you go up four directories to find the prefix. MonoDevelop does this with the following code where up is set to 4.

    static string PathUp (string path, int up)
    {
        if (up == 0)
            return path;
        for (int i = path.Length -1; i >= 0; i--) {
            if (path[i] == Path.DirectorySeparatorChar) {
                up--;
                if (up == 0)
                    return path.Substring (0, i);
            }
        }
        return null;
    }

string prefix = PathUp (typeof (int).Assembly.Location, 4);

This will give you the prefix, usually /usr or /usr/local, and then you can append bin to find the directory where Mono is being run from.

Matt Ward
  • 47,057
  • 5
  • 93
  • 94