2010-01-08 63 views
1

我需要一种方法来唯一标识一台使用.NET的计算机,它既适用于Windows也适用于Linux。唯一标识使用.NET/Mono的计算机?

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

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

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

回答

1

不,WMI不适用于单声道。

3

我正在使我们的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,但每次都失败。在Ubuntu 9.10上测试过。希望这能让你朝着正确的方向前进。

+1

只需检查http://msdn.microsoft.com/en-us/library/system.operatingsystem.platform.aspx而不是Type.GetType(“Mono.Runtime”)。 – skolima 2010-01-11 10:59:44