C#編寫的用光碟片做啟動盤全程描述

來源:互聯網
上載者:User
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。




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.