我需要一种方法来唯一标识一台使用 .NET 的计算机,该 .NET 可在 Windows 和 Linux 中运行。
两个操作系统中使用的方法不必相同。也就是说:我可以有一种方法让 Mono 在 Linux 上运行,而另一种方法让 .NET 在 Windows 上运行。
我看了另一个similar question在 SO 上,似乎使用 .NET 识别计算机的默认方法是使用 WMI,但是无论操作系统是什么,它都可以在 Mono 机器上工作吗?
我乐于接受建议。提前致谢。
最佳答案
我正在让我们的一个 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 上测试。希望这能让您朝着正确的方向前进。
关于c# - 使用.NET/Mono 唯一标识一台计算机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2026838/