2010-01-08 79 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