⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formmain.cs

📁 C#开发的驱动备份程序供大家参考 C#开发的驱动备份程序供大家参考
💻 CS
📖 第 1 页 / 共 2 页
字号:
      this.Load += new System.EventHandler(this.FormMain_Load);
      this.groupBox1.ResumeLayout(false);
      this.ResumeLayout(false);

    }
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new FormMain());
		}

    private void FormMain_Load(object sender, System.EventArgs e) {
      LoadDriverList(false);
    }

    private void LoadDriverList(bool SystemDriver){
      statusBar1.Text = "正在搜索..";
      DriverListView.Items.Clear();
      bool selectBool = false;
      RegistryHelper rh = new RegistryHelper();
      string[] s = rh.LoadLocalMachineControlNames();
      for(int i=0;i<s.Length;i++){
        string driverKey = @"\SYSTEM\CurrentControlSet\Control\Class\" + s[i];
        RegistryKey rk = rh.OpenRegistryKey(driverKey);
        if(rk!=null){
          string[] drivers = rh.LoadLocalMachineControlNames(rk);
          rk.Close();
          if(drivers.Length>0){
            for(int j=0;j<drivers.Length;j++){
              if(drivers[j].Length<=4){
                statusBar1.Text = "正在读取设备信息..";
                string driverKeys = driverKey + "\\" + drivers[j];
                rk = rh.OpenRegistryKey(driverKeys);
                //驱动设备名称
                string driverDesc = rk.GetValue("DriverDesc","").ToString().Trim();
                //驱动发布厂商信息
                string providerName = rk.GetValue("ProviderName","Microsoft").ToString().Trim();
                rk.Close();

                //驱动类型
                rk = rh.OpenRegistryKey(driverKey);
                string driverType = rk.GetValue("","").ToString().Trim();
                if(!SystemDriver){
                  selectBool = (driverDesc!="") && (providerName!="Microsoft");
                }else{
                  selectBool = (driverDesc!="");
                }
                rk.Close();

                if(selectBool){                  
                  bool find =false;
                  for(int n=0;n<DriverListView.Items.Count;n++){
                    find = DriverListView.Items[n].SubItems[0].Text.ToLower()==driverDesc.ToLower();
                    if(find)break;
                  }
                  if(!find){
                    ListViewItem lvi =new ListViewItem(driverDesc);
                    lvi.SubItems.Add(driverType);
                    lvi.SubItems.Add(providerName!="Microsoft" ? "建议备份" : "系统自带");
                    lvi.SubItems.Add(driverKeys);
                    DriverListView.Items.Add(lvi);
                  }
                }
              }
            }
          }
        }
      }
      statusBar1.Text = "完成设备搜索..";
    }
    private void miScan_Click(object sender, System.EventArgs e) {
      LoadDriverList(false);
    }

    private void miScanAll_Click(object sender, System.EventArgs e) {
      LoadDriverList(true);
    }

    private void miExit_Click(object sender, System.EventArgs e) {
      this.Close();
    }

    private void DriverListView_SelectedIndexChanged(object sender, System.EventArgs e) {
      if(DriverListView.SelectedItems!=null){
        foreach(ListViewItem lvi in DriverListView.SelectedItems){
          if(lvi.Selected){
            string driverKey = lvi.SubItems[3].Text;
            RegistryHelper rh = new RegistryHelper();
            RegistryKey rk = rh.OpenRegistryKey(driverKey);
            label1.Text = "驱动设备名称:" + rk.GetValue("DriverDesc","").ToString();
            label2.Text = "驱动发布厂商:" + rk.GetValue("ProviderName","").ToString();
            label3.Text = "驱动程序版本:" + rk.GetValue("DriverVersion","").ToString();
            label4.Text = "驱动发布日期:" + rk.GetValue("DriverDate","").ToString();

            string keyPath = rk.GetValue("InfPath","").ToString();

            string windir = System.Environment.SystemDirectory;
            if(windir.Substring(windir.Length-1,1)=="\\"){
              windir = windir.Substring(0,windir.Length-1);
            }
            windir = windir.Substring(0,windir.LastIndexOf("\\")) + "\\inf\\" + keyPath;
            IniFile iniread = new IniFile(windir);
            try{
              label5.Text = "微软数字签名:" + iniread.ReadString("Version","CatalogFile","未经数字签名");
              System.Collections.Specialized.StringCollection section = new System.Collections.Specialized.StringCollection();
              iniread.ReadSection("SourceDisksFiles",section);
              label6.Text = "驱动程序文件:" + (section.Count+1).ToString() + " 个文件";
            }finally{
              iniread.UpdateFile();
            }
            rk.Close();

          }            
        }
      }
    }

    private ArrayList SearchDriverFiles(string InfPath){

      System.Collections.ArrayList result = new ArrayList();
      IniFile iniread = new IniFile(InfPath);
      try{
        System.Collections.Specialized.StringCollection section = new System.Collections.Specialized.StringCollection();
        iniread.ReadSection("SourceDisksFiles",section);
        string systemPath = System.Environment.SystemDirectory;
        string windowsPath = System.Environment.SystemDirectory.Substring(0,System.Environment.SystemDirectory.LastIndexOf("\\"));

        if(section.Count>0)
          for(int i=0;i<section.Count;i++){
            string ext = section[i].Substring( section[i].LastIndexOf(".") );
            string searchPath = "";
            switch(ext.ToUpper()){
              case ".exe":
                searchPath = systemPath;
                break;
              case ".dll":
                searchPath = systemPath;
                break;
              case ".sys":
                searchPath = systemPath + "\\drivers\\";
                break;
              case ".hlp":
                searchPath = windowsPath + "\\help\\";
                break;
              case ".chm":
                searchPath = windowsPath + "\\help\\";
                break;
              case ".icm":
                searchPath = systemPath + "\\spool\\drivers\\color\\";
                break;
              case ".inf":
                searchPath = windowsPath + "\\inf\\";
                break;
              case ".tvp":
                searchPath = windowsPath + "\\nview\\";
                break;
              default:
                searchPath = systemPath;
                break;
            }

            //搜索当前文件
            if(searchPath.Substring(searchPath.Length-1,1)!="\\")
              searchPath += "\\";
            if (System.IO.File.Exists(searchPath + section[i])){
              result.Add(searchPath + section[i]);
            }

          }
        return result;        
      }catch{
        return null;
      }finally{
        iniread.UpdateFile();
      }
    }

    private void miBackSel_Click(object sender, System.EventArgs e) {
      if(DriverListView.Items!=null)
        foreach(ListViewItem lvi in DriverListView.Items)
          if(lvi.Checked){
            string driverKey = lvi.SubItems[3].Text;
            RegistryHelper rh = new RegistryHelper();
            RegistryKey rk = rh.OpenRegistryKey(driverKey);
            string keyPath = rk.GetValue("InfPath","").ToString();

            string windir = System.Environment.SystemDirectory;
            if(windir.Substring(windir.Length-1,1)=="\\"){
              windir = windir.Substring(0,windir.Length-1);
            }
            windir = windir.Substring(0,windir.LastIndexOf("\\")) + "\\inf\\" + keyPath;
            rk.Close();

            statusBar1.Text = "正在搜索..";
            ArrayList al = SearchDriverFiles(windir);

            statusBar1.Text = "开始备份..";
            string myDriver = Application.ExecutablePath.Substring(0,Application.ExecutablePath.LastIndexOf("\\")) + "\\Drivers";

            if(!Directory.Exists(myDriver)){
              Directory.CreateDirectory(myDriver);
            }

            string myDriverName = myDriver + "\\" + lvi.SubItems[0].Text.Replace("?","_");
            if(!Directory.Exists(myDriverName)){
              Directory.CreateDirectory(myDriverName);
            }
            
            if(al!=null){
              for(int i=0;i<al.Count;i++){
                string filename = al[i].ToString().Substring(al[i].ToString().LastIndexOf("\\"));
                File.Copy(al[i].ToString(),myDriverName + filename,true);
                statusBar1.Text = "备份.." + filename;
              }
            }
            statusBar1.Text = "备份" + lvi.SubItems[0].Text.Replace("?","_") + " 完成";

          }

      statusBar1.Text = "备份全部完成";
    }

    private void miBackDef_Click(object sender, System.EventArgs e) {
      if(DriverListView.Items!=null)
        foreach(ListViewItem lvi in DriverListView.Items)
          if(lvi.SubItems[2].Text=="建议备份"){
            string driverKey = lvi.SubItems[3].Text;
            RegistryHelper rh = new RegistryHelper();
            RegistryKey rk = rh.OpenRegistryKey(driverKey);
            string keyPath = rk.GetValue("InfPath","").ToString();

            string windir = System.Environment.SystemDirectory;
            if(windir.Substring(windir.Length-1,1)=="\\"){
              windir = windir.Substring(0,windir.Length-1);
            }
            windir = windir.Substring(0,windir.LastIndexOf("\\")) + "\\inf\\" + keyPath;
            rk.Close();

            statusBar1.Text = "正在搜索..";
            ArrayList al = SearchDriverFiles(windir);

            statusBar1.Text = "开始备份..";
            string myDriver = Application.ExecutablePath.Substring(0,Application.ExecutablePath.LastIndexOf("\\")) + "\\Drivers";

            if(!Directory.Exists(myDriver)){
              Directory.CreateDirectory(myDriver);
            }

            string myDriverName = myDriver + "\\" + lvi.SubItems[0].Text.Replace("?","_");
            if(!Directory.Exists(myDriverName)){
              Directory.CreateDirectory(myDriverName);
            }
            
            if(al!=null){
              for(int i=0;i<al.Count;i++){
                string filename = al[i].ToString().Substring(al[i].ToString().LastIndexOf("\\"));
                File.Copy(al[i].ToString(),myDriverName + filename,true);
                statusBar1.Text = "备份.." + filename;
              }
            }
            statusBar1.Text = "备份" + lvi.SubItems[0].Text.Replace("?","_") + " 完成";

          }

      statusBar1.Text = "备份全部完成";
    }

    private void miBackAll_Click(object sender, System.EventArgs e) {
      if(DriverListView.Items!=null)
        foreach(ListViewItem lvi in DriverListView.Items){
          string driverKey = lvi.SubItems[3].Text;
          RegistryHelper rh = new RegistryHelper();
          RegistryKey rk = rh.OpenRegistryKey(driverKey);
          string keyPath = rk.GetValue("InfPath","").ToString();

          string windir = System.Environment.SystemDirectory;
          if(windir.Substring(windir.Length-1,1)=="\\"){
            windir = windir.Substring(0,windir.Length-1);
          }
          windir = windir.Substring(0,windir.LastIndexOf("\\")) + "\\inf\\" + keyPath;
          rk.Close();

          statusBar1.Text = "正在搜索..";
          ArrayList al = SearchDriverFiles(windir);

          statusBar1.Text = "开始备份..";
          string myDriver = Application.ExecutablePath.Substring(0,Application.ExecutablePath.LastIndexOf("\\")) + "\\Drivers";

          if(!Directory.Exists(myDriver)){
            Directory.CreateDirectory(myDriver);
          }

          string myDriverName = myDriver + "\\" + lvi.SubItems[0].Text.Replace("?","_");
          if(!Directory.Exists(myDriverName)){
            Directory.CreateDirectory(myDriverName);
          }
            
          if(al!=null){
            for(int i=0;i<al.Count;i++){
              string filename = al[i].ToString().Substring(al[i].ToString().LastIndexOf("\\"));
              File.Copy(al[i].ToString(),myDriverName + filename,true);
              statusBar1.Text = "备份.." + filename;
            }
          }
          statusBar1.Text = "备份" + lvi.SubItems[0].Text.Replace("?","_") + " 完成";
        }
      statusBar1.Text = "备份全部完成";
    }

    private void menuItem1_Click(object sender, System.EventArgs e) {
      if(DriverListView.Items !=null)
        foreach(ListViewItem lvi in DriverListView.Items){
          lvi.Checked = true;
        }
    }

    private void menuItem3_Click(object sender, System.EventArgs e) {
      if(DriverListView.Items !=null)
        foreach(ListViewItem lvi in DriverListView.Items){
          lvi.Checked = !lvi.Checked;
        }
    }

    [DllImport("shell32.dll", EntryPoint="ShellAbout")]
    public static extern int ShellAbout (
      int hwnd,
      string szApp,
      string szOtherStuff,
      int hIcon
      );

    private void menuItem6_Click(object sender, System.EventArgs e) {
      ShellAbout(this.Handle.ToInt32(),this.Text,"S.F. mailluck@gmail.com",this.Icon.Handle.ToInt32());
    }

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -