UUID含義是通用惟一識別碼 (Universally Unique Identifier),這 是一個軟件建構的標準,也是被開源軟件基金會 (Open Software Foundation, OSF) 的組織應用在分佈式計算環境 (Distributed Computing Environment, DCE) 領域的一部分。windows
組成
UUID是指在一臺機器上生成的數字,它保證對在同一時空中的全部機器都是惟一的。一般平臺會提供生成的API。按照開放軟件基金會(OSF)制定的標準計算,用到了以太網卡地址、納秒級時間、芯片ID碼和許多可能的數字 安全
UUID由如下幾部分的組合: 服務器
(1)當前日期和時間,UUID的第一個部分與時間有關,若是你在生成一個UUID以後,過幾秒又生成一個UUID,則第一個部分不一樣,其他相同。 異步
(2)時鐘序列。 分佈式
(3)全局惟一的IEEE機器識別號,若是有網卡,從網卡MAC地址得到,沒有網卡以其餘方式得到。 函數
UUID的惟一缺陷在於生成的結果串會比較長。關於UUID這個標準使用最廣泛的是微軟的GUID(Globals Unique Identifiers)。在ColdFusion中能夠用CreateUUID()函數很簡單地生成UUID,其格式爲:xxxxxxxx-xxxx- xxxx-xxxxxxxxxxxxxxxx(8-4-4-16),其中每一個 x 是 0-9 或 a-f 範圍內的一個十六進制的數字。而標準的UUID格式爲:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12),能夠從cflib 下載CreateGUID() UDF進行轉換。測試
-------以上內容摘自《百度百科》ui
由於軟件產品中須要與硬件碼進行綁定,就想到了UUID,經過百度,網上搜索了一堆以後,發現大部分的代碼都是以下:spa
須要引用:System.Management;操作系統
string processor = "Win32_Processor";//類名 ManagementClass driveClass= new ManagementClass(processor); Console.WriteLine(driveClass.GetQualifierValue("UUID"));
而後,讓咱們部門全部同事在各自的電腦上運行了一次,發現結果以下:
所有運行的結果都是相同的。(這是爲何呢??到如今我也不知道,但不甘心,繼續搜Google)
----------------------------------------------我是分隔線-----------------------------------------------
功夫不負有心人,後來查資料發現,Windows PowerShell也能夠獲取UUID,雖然對於PowerShell我也不熟悉,但核心是能不能解決個人問題?
Windows PowerShell 是一種命令行外殼程序和腳本環境,使命令行用戶和腳本編寫者能夠利用 .NET Framework 的強大功能。
它引入了許多很是有用的新概念,從而進一步擴展了您在 Windows 命令提示符和 Windows Script Host 環境中得到的知識和建立的腳本。
首先,你必須保證操做系統上有PowerShell安裝在您的系統上,另外Vs開發工程中須要引用 System.Management.Automation.dll,
這個dll在我電腦如下路徑裏:「 C:\windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\」,
本機操做系統:Win7
核心的代碼以下:
private static string GetUUID() { try { string uuid = string.Empty; using (PowerShell PowerShellInstance = PowerShell.Create()) { PowerShellInstance.AddScript("(get-wmiobject Win32_ComputerSystemProduct).UUID"); //OK Collection<PSObject> PSOutput = PowerShellInstance.Invoke(); foreach (PSObject outputItem in PSOutput) { if (outputItem != null) { uuid += outputItem.BaseObject.ToString(); } } } return uuid; } catch { return string.Empty; } }
其調用其實就是使用PowerShell的Script進行獲取。由於在調用PowerShell時,可能會比較的慢,.net中也提供了異步調用的機制。核心代碼以下:
private static string GetAsyncUUID() { try { string uuid = string.Empty; using (PowerShell PowerShellInstance = PowerShell.Create()) { PowerShellInstance.AddScript("(get-wmiobject Win32_ComputerSystemProduct).UUID"); //OK PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>(); outputCollection.DataAdded += outputCollection_DataAdded; PowerShellInstance.Streams.Error.DataAdded += Error_DataAdded; IAsyncResult result = PowerShellInstance.BeginInvoke<PSObject, PSObject>(null, outputCollection); while (result.IsCompleted == false) { Console.WriteLine("Waiting for pipeline to finish..."); Thread.Sleep(1000); // While裏面能夠寫上執行等待中的一些事情 } foreach (PSObject outputItem in outputCollection) { if (outputItem != null) { uuid += outputItem.BaseObject.ToString(); } } } return uuid; } catch { return string.Empty; } }
static void Error_DataAdded(object sender, DataAddedEventArgs e) { Console.WriteLine("An error was written to the Error stream!"); } static void outputCollection_DataAdded(object sender, DataAddedEventArgs e) { Console.WriteLine("Object added to output."); }
以上代碼運行以後,通過測試以後,部門沒有重複的。結果以下:
暫時,從以上測試結果分析來看,這個方法是可行的。但目前仍然有比較擔憂的幾個問題:
一、PowerShell在不一樣的版本里面,調用的方法會不會不同?由於作爲B/s軟件須要考慮更多的Windows服務器?
好比: (get-wmiobject Win32_ComputerSystemProduct).UUID
二、爲了安全,PowerShell會不會被服務器給禁用?
三、由於B/s軟件是須要IIS來運行的,會不會出現權限不足的狀況??
但願有對Windows PowerShell比較熟悉的朋友能指教一下。
另:本文僅做記錄分享,髒水請勿亂吐!
===============================補充:==========================================
後期通過大量測試發現:windows Server 2003 使用PowerShell 獲取UUID,可能會報錯,因此建議仍是使用以下方式:
var c = new ManagementClass("Win32_ComputerSystemProduct"); var instance = c.GetInstances(); foreach (var i in instance) Console.WriteLine(i["uuid"]);