[转]C# 获取硬盘序列号 Volume Serial Number

在做软件注册时,通常用硬盘号(建议用散列后的硬盘号)作为本地电脑特征码,加上用户名以及公司名等其他信息,通过一定的算法,得到软件序列号。这样做的好处的显而易见的。它可以防止一个序列号N多人用的现象。现在有些软件就是一个注册码,在网上公开,全世界人都在用。但是也有相应的缺陷。客户只能在一台电脑上用你写的软件。下面的方法通过Windows API获得硬盘号。
using System.Runtime.InteropServices;
[DllImport(
"kernel32.dll")]
        
private static extern int GetVolumeInformation( 
            
string lpRootPathName, 
            
string lpVolumeNameBuffer, 
            
int nVolumeNameSize, 
            
ref int lpVolumeSerialNumber, 
            
int lpMaximumComponentLength, 
            
int lpFileSystemFlags, 
            
string lpFileSystemNameBuffer, 
            
int nFileSystemNameSize 
            ); 

        
private string GetVolOf(string drvID)
            
const int MAX_FILENAME_LEN = 256
            
int retVal = 0
            
int a =0
            
int b =0
            
string str1 = null
            
string str2 = null
            
int i = GetVolumeInformation( 
                drvID 
+ @":"
                str1, 
                MAX_FILENAME_LEN, 
                
ref retVal, 
                a, 
                b, 
                str2, 
                MAX_FILENAME_LEN 
                ); 

            
return retVal.ToString("x"); 
        }
 

 调用方法:例如C盘:GetVolOf("C");

不过无论你的注册算法如何精妙,始终会被破解。因为你的算法始终在软件客户端,cracker总是有机会找到你的注册算法,做出注册机来。看看网上流传的五花八门的注册机就知道了。个人觉得如果做依赖Web的程序,最好还是把注册算法写在Web Service里面,这样cracker就没法从本地破解注册算法了,这样就大大增加了安全性。

原文地址:https://www.cnblogs.com/wangpei/p/1337510.html