代码之家  ›  专栏  ›  技术社区  ›  Andre Pena

使用.net/mono唯一标识计算机?

  •  1
  • Andre Pena  · 技术社区  · 15 年前

    我需要一种方法来唯一地识别一台在windows和linux下都能工作的使用.net的计算机。

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

    我看了另一个 similar question 在so上,用.net来识别计算机的默认方法似乎是使用wmi,但是无论操作系统是什么,这种方法在mono机器上都有效吗?

    我乐于接受建议。提前谢谢。

    3 回复  |  直到 8 年前
        1
  •  3
  •   JohnForDummies    15 年前

    我正在让我们的一个c/.net应用程序在linux上运行…所以我知道你在经历什么,必须想办法克服Linux和Windows之间的差异。

    现在这是非常,非常老套的,这只是一个粗略的草案,我为你一起。也许有更好的方法,但也许它会给你一个主意…

    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;
    }
    

    我尝试将其加载到数据集而不是xmldocument中,但每次都失败。在Ubuntu9.10上测试。希望这能让你走对方向。

        2
  •  1
  •   jpobst    15 年前

    不,wmi不能在mono上工作。

        3
  •  1
  •   Community CDub    8 年前

    会得到 MAC address 工作?

    推荐文章