C#编写的用光盘做启动盘全程描述

本文详细介绍了如何使用C#语言编写程序,通过WMI获取光盘序列号并写入注册表来创建启动盘。同时,文章还讲解了验证启动盘的方法,即比较当前光盘序列号与注册表中的序列号是否一致,以确保程序正常启动。代码已在Visual Studio .NET 2003环境下测试通过。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C#编写的用光盘做启动盘全程描述
作者:thinkersky? (Email: yanghuangming@rongji.com)
一 :编程思想
?? 1、创建启动盘
?????? 插入要创建的启动盘,程序自动检测光驱中光盘,利用WMI(Windows管理架构:Windows Management Instrumentation)读取该光盘的序列号(具有唯一性),把该序列号写入注册表。
2、验证
?? 程序执行时,自动检测光驱中的光盘,利用WMI获取序列号,然后读取注册表中先前写入的序列号,二者比较,相同则程序启动成功,否则提示插入启动盘。
二 :相关知识
?? 1、 什么是WMI?
?WMI(Windows管理架构:Windows Management Instrumentation)是Microsoft基于Web的企业管理(WBEM)和 Desktop Management Task Force(DMTF)工业标准的实现. 就是一种基于标准的系统管理的开发接口,这组接口用来控制管理计算机. 它提供了一种简单的方法来管理和控制系统资源.如果你想深入了解他,可以参考Micorosft Platform SDK . 在这我们只是通过它实现一个简单的功能,? 得到我们系统中光盘的相关信息.[ 我们需要使用System.Management名字空间下提供的类来实现.]。
? 2、 如何在C#中操作注册表
????? 使用VC,VB等语言操作注册表的例子已经有很多了,其实在C#里操作注册表更加的简单方便。下面的例子就提供了在C#里操作注册表的方法:
??? using Microsoft.Win32;
??? using System.Diagnostics;
??? private void Access_Registry()
??? {
???????? // 在HKEY_LOCAL_MACHINE/Software下建立一新键,起名为CDDriveSn
??????????? RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
???????? // 增加一个子键
??????????? RegistryKey newkey = key.CreateSubKey("CDDriveSn");
???????? // 设置此子键的值
??????????? newkey.SetValue("CDDriveSn", "123456789");
???????? // 从注册表的其他地方获取数据
???????? // 找出你的CPU
??????????? RegistryKey pRegKey = Registry.LocalMachine;
????????? pRegKey= pRegKey.OpenSubKey("HARDWARE//DESCRIPTION//System//CentralProcessor//0");
??????????? Object val = pRegKey.GetValue("VendorIdentifier");
??????????? Debug.WriteLine("The central processor of this machine is:"+ val);
???????? // 删除键值
??????????? RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software", true);
??????????? delKey.DeleteSubKey("CDDriveSn");
}
三、编写代码如下
?1、创建启动盘

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;
using System.Diagnostics;
using Microsoft.Win32;

namespace AT_RegCDRom
{
?///


?/// Form1 的摘要说明。
?///

?public class Form1 : System.Windows.Forms.Form
?{
??///
??/// 必需的设计器变量。
??///

??private System.ComponentModel.Container components = null;
??private System.Windows.Forms.Label label1;
??private System.Windows.Forms.Button button1;
??????? private static string cdRomSn;
???????
??public Form1()
??{
???//
???// Windows 窗体设计器支持所必需的
???//
???InitializeComponent();

???//
???// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
???//
??}

??///


??/// 清理所有正在使用的资源。
??///

??protected override void Dispose( bool disposing )
??{
???if( disposing )
???{
????if (components != null)
????{
?????components.Dispose();
????}
???}
???base.Dispose( disposing );
??}

??#region Windows 窗体设计器生成的代码
??///


??/// 设计器支持所需的方法 - 不要使用代码编辑器修改
??/// 此方法的内容。
??///

??private void InitializeComponent()
??{
???System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
???this.label1 = new System.Windows.Forms.Label();
???this.button1 = new System.Windows.Forms.Button();
???this.SuspendLayout();
???//
???// label1
???//
???this.label1.Location = new System.Drawing.Point(72, 16);
???this.label1.Name = "label1";
???this.label1.Size = new System.Drawing.Size(144, 24);
???this.label1.TabIndex = 0;
???this.label1.Text = "点击开始进行光盘注册";
???this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
???//
???// button1
???//
???this.button1.Location = new System.Drawing.Point(104, 56);
???this.button1.Name = "button1";
???this.button1.Size = new System.Drawing.Size(72, 24);
???this.button1.TabIndex = 1;
???this.button1.Text = "开始注册";
???this.button1.Click += new System.EventHandler(this.button1_Click);
???//
???// Form1
???//
???this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
???this.ClientSize = new System.Drawing.Size(280, 101);
???this.Controls.Add(this.button1);
???this.Controls.Add(this.label1);
???this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
???this.MaximizeBox = false;
???this.Name = "Form1";
???this.Text = "光盘注册";
???this.ResumeLayout(false);

??}
??#endregion

??///


??/// 应用程序的主入口点。
??///

??[STAThread]
??static void Main()
??{
???Application.Run(new Form1());
??}

??///


??/// 读取光盘相关信息并进行注册。
??///

??public void ReadCDRom()
??{
???//创建获取光盘信息对象
???ManagementClass driveClass = new ManagementClass("Win32_CDROMDrive");

      //返回该类的所有实例的集合    
???ManagementObjectCollection drives = driveClass.GetInstances();?
      
???//设置状态标志位初始化为false
???bool status = false;
???????????
???//查询所有的光驱
???foreach (ManagementObject drv in drives)
???{ ?
????try
????{
?????//优先获取第一个有光盘的光驱中光盘的序列号
?????if(drv["VolumeSerialNumber"].ToString()!="")
?????{?
??????cdRomSn = drv["VolumeSerialNumber"].ToString();
??????status=true;
??????//查询结束
??????break;
?????}
?????
????}
????catch
????{
?????//出现异常

?????status=false;
?????????
?????continue;
????}

???}
???????????
??
???if(status)
???{
??????????????? //开始注册
????if(RegCDRomSn(cdRomSn))
????{
?????this.label1.Text = "注册成功!";
?????this.button1.Text = "关闭";
????}
????else
????{
????? this.label1.Text = "注册失败!";
?????this.button1.Text = "关闭";
????}
?????????
???}
???else
???{
????// Initializes the variables to pass to the MessageBox.Show method.

????string message = "请插入要注册的启动光盘!";
????string caption = "光盘注册";
????MessageBoxButtons buttons = MessageBoxButtons.OKCancel;
????DialogResult result;

????// Displays the MessageBox.

????result = MessageBox.Show(this, message, caption, buttons,
?????MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
????if(result==DialogResult.OK)
????{
?????ReadCDRom();
????}
????else
????{
?????Application.Exit();
????}
???
???}
?
???driveClass.Dispose();??
??
??}
??///


??/// 把信息写入注册表。
??///

??public bool RegCDRomSn(string sn)
??{
???try
???{
????// 在HKEY_LOCAL_MACHINE/Software下建立一新键,起名为CDDriveSn??????
????RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
????// 增加一个子键
????RegistryKey newkey = key.CreateSubKey("CDDriveSn");
????// 设置此子键的值
????newkey.SetValue("CDDriveSn", sn);
??????????????? // 成功返回true
????return true;

???}
???catch
???{
????// 出现异常返回false
????? return false;
???}

??}

??private void button1_Click(object sender, System.EventArgs e)
??{
???if(this.button1.Text == "开始注册")
???{
????this.button1.Text = "取消注册";?
??  ??? ReadCDRom();
???
???}
???else
???{
?????????????? Application.Exit();
???}

??}
?}
}

2、验证[应用]
? 该验证原理与注册一样,所以,这里不在做过多描述,要代码的朋友请发信到我的邮箱[yanghuangming@rongji.com]。
以上代码在Visual Studio .NET 2003环境下运行通过。这是俺在为一家反光盘盗版公司开发一个应用程序中所用到的一些东东,这里拿出来与大家一起探讨,如有啥不足之处,欢迎指教,3Q。
???????????????????????????
????????????????????????????????????????????

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值