获取 ClickOnce 的安装 uid
Get ClickOnce's installation uid
我正在开发一个通过 ClickOnce 部署的应用程序,我需要能够区分每个安装。
有没有办法获得某种部署 ID,该 ID 每次安装都会更改(因此同一台 PC 上的 2 个用户获得 2 个不同的 ID)但如果更新应用程序则保持不变?
谢谢
经过更多研究后,我找不到任何东西,所以我最终通过散列 CPU id、主板信息和用户 GUID 自己创建了 UID。
从 CodeProject article and a MSDN Forum question
中得到一些灵感
Public Function SHA256Hash(ByVal s As String) As String
Dim hashFunction As SHA256 = SHA256Managed.Create
Dim bytes() As Byte = (New ASCIIEncoding).GetBytes(s)
Dim hash() As Byte = hashFunction.ComputeHash(bytes)
Return GetStringFromHash(hash)
End Function
Private _HardwareID As String
Public Function HardwareID() As String
If String.IsNullOrWhiteSpace(_HardwareID) Then
_HardwareID = SHA256Hash(String.Format("CPU>>{0}|BASE>>{1}|USER>>{2}", cpuID, baseID, userID))
End If
Return _HardwareID
End Function
Private Function identifier(ByVal wmiClass As String, ByVal wmiProperty As String)
Dim result As String = String.Empty
Dim mc As New Management.ManagementClass(wmiClass)
Dim moc As Management.ManagementObjectCollection = mc.GetInstances()
For Each mo As Management.ManagementObject In moc
'Only get the first one
If String.IsNullOrWhiteSpace(result) Then
Try
result = mo(wmiProperty).ToString
Catch ex As Exception
End Try
End If
Next
Return result
End Function
Private Function cpuID() As String
'Uses first CPU identifier available in order of preference
'Don't get all identifiers, as it is very time consuming
Dim retVal As String = identifier("Win32_Processor", "UniqueID")
If String.IsNullOrWhiteSpace(retVal) Then 'If no UniqueID, use ProcessorID
retVal = identifier("Win32_Processor", "ProcessorId")
If String.IsNullOrWhiteSpace(retVal) Then 'If no ProcessorId, use Name
retVal = identifier("Win32_Processor", "Name")
If String.IsNullOrWhiteSpace(retVal) Then 'If no Name, use Manufacturer
retVal = identifier("Win32_Processor", "Manufacturer")
End If
End If
End If
Return retVal
End Function
Private Function baseID() As String
Return String.Concat(identifier("Win32_BaseBoard", "Model"), _
identifier("Win32_BaseBoard", "Manufacturer"), _
identifier("Win32_BaseBoard", "Name"), _
identifier("Win32_BaseBoard", "SerialNumber"))
End Function
Private Function userID() As String
Return System.DirectoryServices.AccountManagement.UserPrincipal.Current.Guid.Value.ToString
End Function
我正在开发一个通过 ClickOnce 部署的应用程序,我需要能够区分每个安装。 有没有办法获得某种部署 ID,该 ID 每次安装都会更改(因此同一台 PC 上的 2 个用户获得 2 个不同的 ID)但如果更新应用程序则保持不变?
谢谢
经过更多研究后,我找不到任何东西,所以我最终通过散列 CPU id、主板信息和用户 GUID 自己创建了 UID。
从 CodeProject article and a MSDN Forum question
中得到一些灵感Public Function SHA256Hash(ByVal s As String) As String
Dim hashFunction As SHA256 = SHA256Managed.Create
Dim bytes() As Byte = (New ASCIIEncoding).GetBytes(s)
Dim hash() As Byte = hashFunction.ComputeHash(bytes)
Return GetStringFromHash(hash)
End Function
Private _HardwareID As String
Public Function HardwareID() As String
If String.IsNullOrWhiteSpace(_HardwareID) Then
_HardwareID = SHA256Hash(String.Format("CPU>>{0}|BASE>>{1}|USER>>{2}", cpuID, baseID, userID))
End If
Return _HardwareID
End Function
Private Function identifier(ByVal wmiClass As String, ByVal wmiProperty As String)
Dim result As String = String.Empty
Dim mc As New Management.ManagementClass(wmiClass)
Dim moc As Management.ManagementObjectCollection = mc.GetInstances()
For Each mo As Management.ManagementObject In moc
'Only get the first one
If String.IsNullOrWhiteSpace(result) Then
Try
result = mo(wmiProperty).ToString
Catch ex As Exception
End Try
End If
Next
Return result
End Function
Private Function cpuID() As String
'Uses first CPU identifier available in order of preference
'Don't get all identifiers, as it is very time consuming
Dim retVal As String = identifier("Win32_Processor", "UniqueID")
If String.IsNullOrWhiteSpace(retVal) Then 'If no UniqueID, use ProcessorID
retVal = identifier("Win32_Processor", "ProcessorId")
If String.IsNullOrWhiteSpace(retVal) Then 'If no ProcessorId, use Name
retVal = identifier("Win32_Processor", "Name")
If String.IsNullOrWhiteSpace(retVal) Then 'If no Name, use Manufacturer
retVal = identifier("Win32_Processor", "Manufacturer")
End If
End If
End If
Return retVal
End Function
Private Function baseID() As String
Return String.Concat(identifier("Win32_BaseBoard", "Model"), _
identifier("Win32_BaseBoard", "Manufacturer"), _
identifier("Win32_BaseBoard", "Name"), _
identifier("Win32_BaseBoard", "SerialNumber"))
End Function
Private Function userID() As String
Return System.DirectoryServices.AccountManagement.UserPrincipal.Current.Guid.Value.ToString
End Function