C# winform利用注册表信息实现开机自动运行软件
2013-09-04 10:53
746 查看
#region C# winform利用注册表信息实现开机自动运行软件 static public string ReigstidFileName = "myRegedit"; static public string RegeditPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\"; private void 开机自动运行程序ToolStripMenuItem_Click(object sender, EventArgs e) { // RegistryKey 类 命名空间: Microsoft.Win32 //获得文件的当前路径 string dir=Directory.GetCurrentDirectory(); //获取可执行文件的全部路径 string exeDir = dir + "\\WinForm_ExampleTeaching.exe"; //判断注册表数据信息是否存在 if (!IsRegeditExist("myForm")) { if (WTRegedit("myForm", exeDir)) { MessageBox.Show("添加成功!"); } else { MessageBox.Show("添加失败!"); } } else { MessageBox.Show("自动运行已存在注册表中!"); } } //读注册表数据信息 public string GetRegistData(string name) //键名称 { string registData; RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); RegistryKey aimdir = software.OpenSubKey(ReigstidFileName, true); registData = aimdir.GetValue(name).ToString(); return registData; } //写注册表数据信息,分别是子键名称和值存放LocalMachine目录下的XXX文件中,其文件名为name的值,值为tovalue的值 public bool WTRegedit(string name, string tovalue) { bool WriteOk = false; try { RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); RegistryKey microsoft = software.OpenSubKey("Microsoft", true); RegistryKey Windows = microsoft.OpenSubKey("Windows", true); RegistryKey CurrentVersion = Windows.OpenSubKey("CurrentVersion", true); //RegistryKey Run = CurrentVersion.OpenSubKey("Run", true); RegistryKey aimdir = CurrentVersion.OpenSubKey("Run", true); aimdir.SetValue(name, tovalue); //Run.SetValue(name, tovalue); WriteOk = true; } catch { WriteOk = false; } return WriteOk; } //修改注册表数据信息 public bool EditRegedit(string name, string tovalue) { bool EditOk = false; try { if (IsRegeditExist(name)) { RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); RegistryKey aimdir = software.CreateSubKey(ReigstidFileName); aimdir.SetValue(name, tovalue); EditOk = true; } } catch { EditOk = false; } return EditOk; } //删除注册表数据信息 public bool DeleteRegist(string name) { bool DeleteOk = false; string[] aimnames; RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); RegistryKey microsoft = software.OpenSubKey("Microsoft", true); RegistryKey Windows = microsoft.OpenSubKey("Windows", true); RegistryKey CurrentVersion = Windows.OpenSubKey("CurrentVersion", true); //RegistryKey Run = CurrentVersion.OpenSubKey("Run", true); RegistryKey aimdir = CurrentVersion.OpenSubKey("Run", true); aimnames = aimdir.GetValueNames(); foreach (string aimKey in aimnames) { if (aimKey == name) { //aimdir.Deletue(name); aimdir.DeleteSubKey(name); DeleteOk = true; } } return DeleteOk; } //判断注册表数据信息是否存在 public bool IsRegeditExist(string name) { bool isExist = false; string[] subkeyNames; RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); RegistryKey microsoft = software.OpenSubKey("Microsoft", true); RegistryKey Windows = microsoft.OpenSubKey("Windows", true); RegistryKey CurrentVersion = Windows.OpenSubKey("CurrentVersion", true); //RegistryKey Run = CurrentVersion.OpenSubKey("Run", true); RegistryKey aimdir = CurrentVersion.OpenSubKey("Run", true); subkeyNames = aimdir.GetValueNames(); foreach (string keyName in subkeyNames) { if (keyName == name) { isExist = true; } } return isExist; } #endregion
相关文章推荐
- c#设置软件开机自动运行,修改注册表
- C# Winform打包部署时添加注册表信息实现开机启动
- C#设置软件开机自动运行的方法(修改注册表)
- c#设置软件开机自动运行,修改注册表
- C#实现winform软件开机自动启动并最小化到系统托盘
- C#实现winform软件开机自动启动并最小化到系统托盘
- C#实现winform软件开机自动启动并最小化到系统托盘
- C#实现winform软件开机自动启动并最小化到系统托盘
- c#设置软件开机自动运行,修改注册表
- C# WinFORM通过注册表实现开机运行
- c#设置软件开机自动运行,修改注册表
- C# Winform打包部署时添加注册表信息实现开机启动
- C# Winform打包部署时添加注册表信息实现开机启动
- c#设置软件开机自动运行,修改注册表
- 【转】C# Winform打包部署时添加注册表信息实现开机启动
- 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#)
- 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#) skyover [原作]
- c#实现开机自动启动,以及解决修改注册表权限不足
- 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#)
- C# WinForm自定程序 安装后开机自动运行