我想配置我的 C# Windows 窗体应用程序,以便在运行应用程序之前,它标识匹配当前机器的硬盘驱动器序列号。如果硬盘序列号与配置的序列号匹配,则运行应用程序,否则什么都不做。
我想让它只在一台机器上运行,以防止重新分发应用程序,因为这是一个定制的应用程序,只为有一些特殊要求的客户开发。
以下代码获取当前机器的硬盘序列号、型号和接口类型。
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("select * from Win32_DiskDrive");
foreach (ManagementObject wmi_HDD in moSearcher.Get())
{
HardDrive hdd = new HardDrive();
hdd.Model = wmi_HDD["Model"].ToString();
hdd.SerialNo = wmi_HDD["SerialNumber"].ToString();
hdd.Type = wmi_HDD["InterfaceType"].ToString();
HDDArrayList.Add(wmi_HDD);
txtHDDModel.Text = hdd.Model;
txtHDDSerialNo.Text = hdd.SerialNo;
txtHDDType.Text = hdd.Type;
}
此代码当前在单击按钮时运行。我希望它在 main 方法之前运行,它可以获取当前机器的硬盘序列号并将其与我的目标序列号(我想要允许的序列号)进行比较。
有没有更好的方法来解决这个问题以及比较过程?
这应该适合你:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (ValidHD() != true)
{
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private static bool ValidHD()
{
string hdSN = String.Empty;
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("select * from Win32_DiskDrive");
foreach (ManagementObject wmi_HDD in moSearcher.Get())
{
hdSN = wmi_HDD["SerialNumber"].ToString();
}
if (hdSN == "Your_SN_Here")
{
return true;
}
else
{
return false;
}
}
}
要按用户名限制使用,您可以使用:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (ValidUser() != true)
{
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private static bool ValidUser()
{
if (System.Environment.UserName == "Your_Username_Here")
{
return true;
}
else
{
return false;
}
}
}
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句