程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#編寫的用光盤做啟動盤全程描述

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

編輯:關於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");
}
三、編寫代碼如下
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、驗證[應用]
該驗證原理與注冊一樣,所以,這裡不在做過多描述,要代碼的朋友請發信到我的郵箱[[email protected]]。
以上代碼在Visual Studio .Net 2003環境下運行通過。這是俺在為一家反光盤盜版公司開發一個應用程序中所用到的一些東東,這裡拿出來與大家一起探討,如有啥不足之處,歡迎指教,3Q。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved