使用.NET / Mono唯一识别计算机?

时间:2010-01-08 10:15:47

标签: c# .net mono guid

我需要一种方法来使用适用于Windows和Linux的.NET来唯一地识别计算机。

两个操作系统中使用的方法不必相同。那就是:我可以在Linux上运行Mono,在Windows上运行.NET的另一种方法。

我在SO上看了另一个similar question,似乎用.NET识别计算机的默认方式是使用WMI,但无论操作系统是什么,它都可以在Mono机器上工作吗?

我愿意接受建议。提前谢谢。

3 个答案:

答案 0 :(得分:3)

我正在让我们的一个C#/ .Net应用程序在Linux上工作...所以我知道你要经历的是什么,必须找到解决Linux和Windows之间差异的方法。

现在这非常,非常hacky,这只是我为你拼凑的草稿。可能有更好的方法,但也许它会给你一个想法...

bool IsRunningMono()
{
    // With our application, it will be used on an embedded system, and we know that
    // Windows will never be running Mono, so you may have to adjust accordingly.
    return Type.GetType("Mono.Runtime") != null;
}

string GetCPUSerial()
{
    string serial = "";

    if(IsRunningMono())
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "lshw";
        startInfo.Arguments = "-xml";
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;

        using(Process p = Process.Start(startInfo))
        {
            p.WaitForExit();
            System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();

            xdoc.Load(new System.Xml.XmlTextReader(new StringReader(p.StandardOutput.ReadToEnd())));

            System.Xml.XmlNodeList nodes = xdoc.SelectNodes("node/node/node/serial");

            if (nodes.Count > 0)
            {
                serial = nodes[0].InnerText;              
            }
        }        
    }
    else
    {
       // process using wmi
    }

    return serial;
}

我尝试将其加载到DataSet而不是XmlDocument中,但每次都失败了。在Ubuntu 9.10上测试过。希望这能让你朝着正确的方向前进。

答案 1 :(得分:1)

不,WMI不适用于Mono。

答案 2 :(得分:1)

MAC address工作吗?