C#实现光盘做启动盘
发布日期:2021-06-30 19:11:46 浏览次数:3 分类:技术文章

本文共 5158 字,大约阅读时间需要 17 分钟。

一 :编程思想
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();
}
}
}
 

转载地址:https://linuxstyle.blog.csdn.net/article/details/1539898 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C# POP3编程
下一篇:C# 2进制、8进制、10进制、16进制...各种进制间的轻松转换

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月22日 05时00分51秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Boundary loss 损失函数 2019-04-30
神经网络调参实战(一)—— 训练更多次数 & tensorboard & finetune 2019-04-30
tensorflow使用tensorboard进行可视化 2019-04-30
神经网络调参实战(二)—— activation & initializer & optimizer 2019-04-30
凸优化 convex optimization 2019-04-30
数据库索引 & 为什么要对数据库建立索引 / 数据库建立索引为什么会加快查询速度 2019-04-30
IEEE与APA引用格式 2019-04-30
research gap 2019-04-30
pytorch训练cifar10数据集查看各个种类图片的准确率 2019-04-30
Python鼠标点击图片,获取点击点的像素坐标 2019-04-30
路径规划(一) —— 环境描述(Grid Map & Feature Map) & 全局路径规划(最优路径规划(Dijkstra&A*star) & 概率路径规划(PRM&RRT)) 2019-04-30
神经网络调参实战(四)—— 加深网络层次 & 批归一化 batch normalization 2019-04-30
数据挖掘与数据分析(三)—— 探索性数据分析EDA(多因子与复合分析) & 可视化(1)—— 假设检验(μ&卡方检验&方差检验(F检验))&相关系数(皮尔逊&斯皮尔曼) 2019-04-30
RRT算法(快速拓展随机树)的Python实现 2019-04-30
路径规划(二) —— 轨迹优化(样条法) & 局部规划(人工势能场法) & 智能路径规划(生物启发(蚁群&RVO) & 强化学习) 2019-04-30
D*算法 2019-04-30
强化学习(四) —— Actor-Critic演员评论家 & code 2019-04-30
RESTful API 2019-04-30
优化算法(四)——粒子群优化算法(PSO) 2019-04-30
数据挖掘与数据分析(三)—— 探索性数据分析EDA(多因子与复合分析) & 可视化(2)——回归分析(最小二乘法&决定系数&残差不相关)&主成分分析&奇异值分解 2019-04-30