国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#實現光盤做啟動盤

2024-07-21 02:24:52
字體:
來源:轉載
供稿:網友
菜鳥學堂:

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

  }
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 罗田县| 西安市| 余庆县| 霍林郭勒市| 抚松县| 新丰县| 垣曲县| 游戏| 同仁县| 金堂县| 法库县| 镇原县| 吴桥县| 泸定县| 米泉市| 宜兰市| 徐水县| 阿巴嘎旗| 民县| 淄博市| 安义县| 永定县| 稷山县| 青州市| 秀山| 武宣县| 泗水县| 吉木乃县| 大港区| 龙海市| 本溪市| 浦县| 富平县| 兰西县| 邵东县| 大英县| 西峡县| 田林县| 都兰县| 邵阳县| 惠来县|