C#实现光盘做启动盘

一 :编程思想
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");
}
三、编写代码如下
创建启动盘

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();
}

}
}
}
(www.softgogo.com)该刻录光盘SDK简单易用,功能强大稳定,包括了DLL和OCX控件调用方式。专门用于刻录数据、音乐、VCD、SVCD、DVD影碟,支持实时刻录、实时DVD视频光盘录像、加密刻录等强大功能。支持现在市场上的所有光盘,包括CD,DVD,蓝光盘片,支持所有光盘的数据追加刻录,支持多台同时刻录,并且支持所有类型的刻录机。在Demo演示版本中提供了所有功能测试,您可以直接试用。 该SDK的售价包含了授权费用和使用费用,是一次性价格,没有其他任何的收费。使用该SDK发布的产品没有任何时间和数量的限制,无开发人员使用数量限制。 本SDK是标准的Windows动态连接库文件,可独立完成所有功能,刻录底层不依赖其它的任何第3方的环境或者文件。能够轻松的被其它windows应用程序及开发工具调用,包括VB、VC、DELPHI、C#等。提供的用户接口友好且使用简单。本SDK编码质量优良,操作速度快,不会占用及耗费多余的临时硬盘空间,刻录所需要的数据操作全部在内存中完成,已经通过了以上提及的所有CD及DVD盘片测试,并且经过了大量的各种类型的刻录机的检验测试。 本SDK提供的主要接口函数功能有: ·得到盘片信息 ·搽除可搽写盘片 ·设置并得到当前盘片的读写速度 ·弹进弹出光盘托盘 ·得到光驱设备的型号及附带的产品信息 ·得到当前设备支持的写类型 ·刻录镜像文件(*.ISO及其他标准格式的镜像文件)到光盘 ·刻录文件及文件夹到光盘 ·追加刻录文件及文件夹到光盘(Multisession disc) ·刻录VCD/SVCD光盘 ·刻录DVD影碟 ·得到当前刻录进度 ·得到当前正在刻录的文件名 ·得到当前刻录过程中的错误信息 ·停止刻录进程。 ·刻录文件及文件夹到镜像文件(*.iso) ·刻录mp3或者wave文件到音乐光盘 ·得到mp3或者wave文件的文件信息 ·保存音乐光盘中的Audio Track为mp3或者wave文件 ·得到光盘的Track信息 ·复制CD/DVD盘片 ·支持ISO和UDF格式的数据光盘刻录 ·刻录自运行光盘(AutoRun) ·实时刻录 ·实时DVD光盘录像 ·加密刻录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值