C # Implementation disc to do the boot disk

Source: Internet
Author: User
Tags bool exit delete key implement query tostring win32

One: Programming ideas
1. Create a Startup disk
Insert the boot disk you want to create, the program automatically detects the CD in the CD drive, and reads the serial number (unique) of the disc using WMI (Windows Management Schema: Windows Management Instrumentation) to write the sequence number to the registry.
2. Verify
When the program executes, automatically detects the disc in the optical drive, obtains the serial number using WMI, and then reads the serial number previously written in the registry, comparing the two, the same program starts successfully, or prompts you to insert the boot disk.
Second: Related knowledge
1. What is WMI?
WMI (Windows Management Schema: Windows Management Instrumentation) is a Microsoft web-based Enterprise Management (WBEM) and Desktop Management Task Force (DMTF) The realization of industrial standard. is a standards-based system Management development interface that is used to control the management of computers. It provides an easy way to manage and control system resources. If you want to know more about him, you can refer to the micorosft Platform SDK. In this we just implement a simple function through it to get information about the discs in our system. [We need to use the classes provided under the System.Management namespace to implement.]
2. How to operate the registry in C #
There are many examples of how to manipulate the registry in languages such as VC,VB, but it is easier to operate the registry in C #. The following example provides a way to manipulate the registry in C #:
Using Microsoft.Win32;
Using System.Diagnostics;
private void Access_registry ()
{
Create a new key under HKEY_LOCAL_MACHINE\Software, named CDDRIVESN
RegistryKey key = Registry.LocalMachine.OpenSubKey ("Software", true);
Add a subkey
RegistryKey Newkey = key. CreateSubKey ("CDDRIVESN");
Set the value of this subkey
Newkey. SetValue ("Cddrivesn", "123456789");
Get data from other places in the registry
Find your CPU.
RegistryKey pregkey = registry.localmachine;
pregkey= Pregkey.opensubkey ("hardware\\description\\system\\centralprocessor\\0");
Object val = pregkey.getvalue ("Vendoridentifier");
Debug.WriteLine ("The" "the" processor of this machine is: "+ val);
Delete key values
RegistryKey Delkey = Registry.LocalMachine.OpenSubKey ("Software", true);
Delkey.deletesubkey ("CDDRIVESN");
}
Three, write code as follows
Create a Startup disk

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
{
///
Summary description of the Form1.
///
public class Form1:System.Windows.Forms.Form
{
///
The required designer variable.
///
Private System.ComponentModel.Container components = null;
Private System.Windows.Forms.Label Label1;
Private System.Windows.Forms.Button button1;
private static string cdromsn;

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent call
//
}

///
Clean up all resources that are in use.
///
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer
///
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
///
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 = "Click to start CD registration";
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 = "Start registration";
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 = "Disc registration";
This. ResumeLayout (FALSE);

}
#endregion

///
The main entry point for the application.
///
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}

///
Read and register information about the disc.
///
public void Readcdrom ()
{
Create a Get Disc information object
ManagementClass Driveclass = new ManagementClass ("win32_cdromdrive");

Returns a collection of all instances of the class
Managementobjectcollection drives = Driveclass.getinstances ();
      
Set status flag bit to False
BOOL status = FALSE;

Query all optical drives
foreach (ManagementObject DRV in drives)
{
Try
{
Priority to get the serial number of the first CD-ROM in the CD-ROM
if (drv["VolumeSerialNumber"]. ToString ()!= "")
{
Cdromsn = drv["VolumeSerialNumber"]. ToString ();
Status=true;
End of query
Break
}

}
Catch
{
An exception occurred

Status=false;

Continue
}

}


if (status)
{
Start Registration
if (Regcdromsn (CDROMSN))
{
This.label1.Text = "Registration Successful!" ";
This.button1.Text = "Close";
}
Else
{
This.label1.Text = "Registration failed!" ";
This.button1.Text = "Close";
}

}
Else
{
Initializes the variables to the MessageBox.Show method.

String message = "Please insert the startup CD you want to register!";
String caption = "Disc registration";
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 ();

}
///
Write the information to the registry.
///
public bool Regcdromsn (string sn)
{
Try
{
Create a new key under HKEY_LOCAL_MACHINE\Software, named CDDRIVESN
RegistryKey key = Registry.LocalMachine.OpenSubKey ("Software", true);
Add a subkey
RegistryKey Newkey = key. CreateSubKey ("CDDRIVESN");
Set the value of this subkey
Newkey. SetValue ("CDDRIVESN", SN);
Success returns True
return true;

}
Catch
{
Unexpected return false
return false;
}

}

private void Button1_Click (object sender, System.EventArgs e)
{
if (This.button1.Text = "Start Registration")
{
This.button1.Text = "Cancel Registration";
Readcdrom ();

}
Else
{
Application.exit ();
}

}
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.