using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using Microsoft.Win32;
namespace Demo14_注册表操作
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
RegistryKey rootKey = Registry.LocalMachine;
string strParth = @"SYSTEM\CurrentControlSet\services\USBSTOR";
RegistryKey OpenKey = rootKey.OpenSubKey(strParth);
if (OpenKey.GetValue("Start").ToString() == "3")
{
rbtnEnable.Checked = true;
}
else if(OpenKey.GetValue("Start").ToString()=="4")
{
rbtnDisable.Checked = true;
}
OpenKey.Close();
rootKey.Close();
}
private void btnApply_Click(object sender, EventArgs e)
{
RegistryKey rootKey = Registry.LocalMachine;
string strParth = @"SYSTEM\CurrentControlSet\services\USBSTOR";
RegistryKey OpenKey = rootKey.OpenSubKey(strParth);
try
{
if (rbtnEnable.Checked == true)
{
OpenKey.SetValue("Start", 3);
MessageBox.Show("USB已启用");
}
else if (rbtnDisable.Checked == true)
{
OpenKey.SetValue("Start", 4);
MessageBox.Show("USB已禁用");
}
OpenKey.Close();
rootKey.Close();
}
catch (Exception ex)
{
string error = ex.ToString();
MessageBox.Show("注册表操作错误");
}
}
}
}