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

时间:2021-01-31 04:12:04

I need a way to uniquely identify a computer using .NET that works in both Windows and Linux.

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

The methods used in both operational system do not have to be the same. That is: I can have an approach for Mono running on Linux and another approach for .NET running on Windows.

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

I took a look at another similar question on SO and it seems that the default way to identify the computer with .NET is using WMI, but will that work in a Mono machine whatever the OS is?

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

I'm open to suggestions. Thanks in advance.

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

3 个解决方案

#1


3  

I'm in the middle of getting one of our C#/.Net applications to work on Linux... So I know what you're going through, having to find ways to get around the differences between Linux and Windows.

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

Now this is very, very hacky, and this is just a rough draft I threw together for you. There's probably a better way to do it, but maybe it will give you an idea...

现在这非常,非常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;
}

I tried loading it into a DataSet instead of an XmlDocument, but it failed every time. Tested on Ubuntu 9.10. Hope this gets you going in the right direction.

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

#2


1  

No, WMI will not work on Mono.

不,WMI不适用于Mono。

#3


1  

Would getting the MAC address work?

获取MAC地址是否有效?

#1


3  

I'm in the middle of getting one of our C#/.Net applications to work on Linux... So I know what you're going through, having to find ways to get around the differences between Linux and Windows.

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

Now this is very, very hacky, and this is just a rough draft I threw together for you. There's probably a better way to do it, but maybe it will give you an idea...

现在这非常,非常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;
}

I tried loading it into a DataSet instead of an XmlDocument, but it failed every time. Tested on Ubuntu 9.10. Hope this gets you going in the right direction.

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

#2


1  

No, WMI will not work on Mono.

不,WMI不适用于Mono。

#3


1  

Would getting the MAC address work?

获取MAC地址是否有效?