简体   繁体   English

mono C#获取正在运行的mono vm的路径

[英]mono C# get the path to the running mono vm

I have a C# program that will be executed by mono . 我有一个将由mono执行的C#程序。 From the C# program, I want to get the path to the mono vm binary that is running the program. 从C#程序,我想获取运行该程序的mono vm二进制文件的路径。 Is there an API for that? 是否有API?

Note that I'm not asking for the path to the C# program, but the path to the mono vm binary (eg /usr/local/bin/mono ). 请注意,我不是在询问C#程序的路径,而是在询问mono vm二进制文件的路径(例如/usr/local/bin/mono )。

You could do what MonoDevelop does to find the location of Mono . 您可以执行MonoDevelop的操作来找到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. 这是基于查找装配件的位置,然后返回四个目录以查找当前正在使用Mono的前缀。

The location of the assembly containing int is first found. 首先找到包含int的程序集的位置。

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. MonoDevelop使用以下代码将其设置为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. 这将为您提供前缀,通常是/ usr或/ usr / local,然后您可以追加bin来查找运行Mono的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM