我用C#写了一个程序,并使用高级安装程序制作了exe文件,它运行良好。但是我希望这个exe文件只在一台电脑上运行,因为有些客户会将该exe文件转发给其他人,我希望防止这种情况并保护我的作品。
在您想要 .exe 文件正常运行的计算机上运行下面的代码(它将显示该计算机的 MAC 地址)。 2. 将此 MAC 地址添加到以下代码中。 3. 将以下代码添加到您的 C# 代码中,以确保它仅在具有正确 MAC 地址(唯一)的计算机上运行。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace OneMachine
{
class Program
{
static void Main(string[] args)
{
string clientMAC = "XX-XX-XX-XX-XX-XX"; // put the correct value here when you know it
string thisComputerMAC = GetMACAddress2();
Console.WriteLine("MAC:" + thisComputerMAC); // remove this when necessary
if (clientMAC == thisComputerMAC)
{
Console.WriteLine("this is the right computer");
} else
{
Console.WriteLine("PROGRAM WONT RUN ON THIS COMPUTER");
}
}
public static string GetMACAddress2()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
String sMacAddress = string.Empty;
foreach (NetworkInterface adapter in nics)
{
if (sMacAddress == String.Empty)// only return MAC Address from first card
{
//IPInterfaceProperties properties = adapter.GetIPProperties(); Line is not required
sMacAddress = adapter.GetPhysicalAddress().ToString();
}
} return sMacAddress;
}
}
}
参考资料:C#离线获取计算机的MAC地址
http://www.codeproject.com/Articles/28678/Generating-Unique-Key-Finger-Print-for-a-Computer
您可以为exe创建安全约束,例如:
您可以使用硬件ID来识别应用程序的用户,从而允许仅一个用户运行您的程序,或者您可以使用许可证系统。