如何用C#修改注册表start值

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("注册表操作错误");
}
}

}
}

private void btnApply_Click(object sender, EventArgs e)
{
RegistryKey rootKey = Registry.LocalMachine;
string strParth = @"SYSTEM\CurrentControlSet\services\USBSTOR";

// RegistryKey OpenKey = rootKey.OpenSubKey(strParth); //你原来的
RegistryKey OpenKey = rootKey.OpenSubKey(strParth, true); //我改的
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("注册表操作错误");
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-04-09
string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
System.IO.File.Copy("应用程序路径(包括程序名)", StartupPath + "执行程序文件名称", true);

//获得文件的当前路径
string dir = Directory.GetCurrentDirectory();

//获取可执行文件的全部路径
string exeDir = dir + "WindowsApplication1.exe";

//获取Run键
RegistryKey key1=Registry.LocalMachine;
RegistryKey key2=key1.CreateSubKey("SOFTWARE");
RegistryKey key3=key2.CreateSubKey("Microsoft");
RegistryKey key4=key3.CreateSubKey("Windows");
RegistryKey key5=key4.CreateSubKey("CurrentVersion");
RegistryKey key6=key5.CreateSubKey("Run");

//在Run键中写入一个新的键值
key6.SetValue("myForm",exeDir);
key6.Close();

//如果要取消的话就将key6.SetValue("myForm",exeDir);改成
//key6.SetValue("myForm",false);
相似回答
大家正在搜