首页 C#设置开机自动启动.doc

C#设置开机自动启动.doc

举报
开通vip

C#设置开机自动启动.docC#设置开机自动启动.doc using Microsoft.Win32; private void Form1_Load(object sender, EventArgs e) { //获取程序执行路径.. string starupPath = Application.ExecutablePath; //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装. RegistryKey loca = Registry.LocalMachin...

C#设置开机自动启动.doc
C#设置开机自动启动.doc using Microsoft.Win32; private void Form1_Load(object sender, EventArgs e) { //获取程序执行路径.. string starupPath = Application.ExecutablePath; //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装. RegistryKey loca = Registry.LocalMachine; RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); try { //SetValue:存储值的名称 run.SetValue("WinForm",starupPath); MessageBox.Show("注册表添加成功!!",""提示 ",MessageBoxButtons.OK,MessageBoxIcon.Information); loca.Close(); } catch(Exception ee) { MessageBox.Show(ee.Message.ToString(),"提示 ",MessageBoxButtons.OK,MessageBoxIcon.Error); } } 是这样,我是想在我做的软件里加个开机启动选项,如果选中就可以开机启动,如果不选中就不能开机启动。现在开机启动的代码有了,我想知道取消开机启动的C#代码怎么写 提问者: 清风泉 - 三级 最佳 答案 八年级地理上册填图题岩土工程勘察试题省略号的作用及举例应急救援安全知识车间5s试题及答案 照着你上述代码写一便 不同的地方就是 把run.setValue("WinForm",starupPath); 换成run.DeleteValue("WinForm"); 解释: run.SetValue("WinForm",starupPath); 对应的,有个DeleteValue函数的,用于删除键值对 C#如何获取注册表某个键的子建 悬赏分:5 - 解决时间:2007-2-1 15:46 比如注册表中的Current User中有Software,那么怎么把Software键下的所有子键一一列举出来呢 谢谢 提问者: sys0i - 一级 最佳答案 由于Visual C#本身没有类库,是通过.Net FrameWork SDK(软件开发包)定义的一些类来实现对注册表的操作的。这就是名称空间Microsoft.Win32中封装的二个类:Registry类和RegistryKey类。在RegistryKey类中定义了二个方法用来创建注册表中的主键、子键和键值。它们是CreateSubValue ( )方法和SetValue ( )方法。那么如何用Visual C#来修改注册信息,在本文中,我们只介绍修改注册表中的键值的方法。而对于主键和子键,由于.Net FrameWork SDK中还没有定义这方面的方法,所以还无法完成安全的修改注册表中的信息。 一(Visual C#创建和修改注册信息要调用的两个方法 (1)CreateSubKey ( String key )方法:此方法是创建以后面的字符串为名称的子键。当然这种方法不仅能够创建子键,在下面介绍的程序中,也通过此种方法来创建一个主键。 (2)SetValue ( String name , String keyvalue )方法:此方法的作用有二,一是可以用来重命名键值的数值,一是可以用来创建新的键值。具体情况为:当打开的子键中,如果存在此键值,就把新值赋给他,实现重命名操作。如果不存在,则创建一个新的键值。 二(程序设计和运行环境以及要准备的工作 I>视窗系统2000服务器版 II>.Net FrameWork SDK Beta 2版 III>由于在程序中,要修改一个已经存在的键值,所以就要预先设置好键值所在的位置。打开注册表的编辑器,在"HKEY_LOCAL_MACHINE"主键下面的"HARDWARE"子键下面创建"aaa"子键并在此子键下面创建一个名称为"bbb"的键值。具体如下图所示: 三(程序的主要功能以及设计的重要步骤 在下面介绍的程序中,主要的功能是用Visual C#在注册表中创建一个主键、一个子键和修改一个指定的键值。其中要创建的子键的结构层次是在主键"HKEY_LOCAL_MACHIN"下面的"HAREWARE"主键下,名称为"ddd",其中包含一个键值,名称为"www",键值的值为"1234"。 其中的要创建的主键的结构层次也是在主键"HKEY_LOCAL_MACHIN"下面的"HAREWARE"主键下,名称为"main",在此主键下面包含一个名称为"sub"的子键和名称为"value"键值,键值的值为"1234"。下面就来着重介绍Visual C#是如何创建和修改这些主键、子键和键值的。 (1)如何创建一个子键,在程序中是结合CreateSubKey ( )方法和SetValue ( )方法来实现的,以下是程序中创建子键的源程序: listBox1.Items.Clear ( ) ; RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "HARDWARE" , true ) ; RegistryKey main1 = software.CreateSubKey ( "main" ) ; RegistryKey ddd = main1.CreateSubKey ( "sub" ) ; ddd.SetValue ( "value" , "1234" ); (2)如何创建一个主键,创建一个主键和创建一个子键的过程大致是差不多的。由于主键包含若干子键,所以在创建主键的时候必须注意他们的层次关系。下面这一段程序,在参考的时候,请注意一下main键和sub键之间的关系。 listBox1.Items.Clear ( ) ; RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ; RegistryKey main1 = software.CreateSubKey ( "main" ) ; RegistryKey ddd = main1.CreateSubKey ( "sub" ) ; ddd.SetValue ( "value" , "1234" ) ; (3)如何修改注册信息。由于注册表中的信息十分重要,所以一般不要对其进行写的操作。也可能是这个原因,在.Net FrameWork SDK 中并没有提供修改注册表键的方法。而只是提供了一个危害性相对较小的方法--SetValue ( ),通过这个方法,我们可以来修改键值。下面程序代码是修改一个指定键值名称的键值。当然由于SetValue( )方法的特性,如果它检测到这个键值不存在,就会创建一个新的键值。 listBox1.Items.Clear ( ) ; RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ; RegistryKey dddw = software.OpenSubKey ( "aaa" , true ) ; dddw.SetValue ( "bbb" , "abcd" ) ; 四(本文中源程序代码( reg.cs )以及编译后的程序运行界面: 以下是程序运行的界面: reg.cs程序代码如下: using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using Microsoft.Win32 ; //导入使用到的名称空间 public class Form1 : Form { private System.ComponentModel.Container components ; private ListBox listBox1 ; private Button button1 ; private Button button2 ; private Button button3 ; private Button button4 ; public Form1 ( ) { InitializeComponent ( ) ; } //清除在程序中使用过的资源 public override void Dispose ( ) { base.Dispose ( ) ; components.Dispose ( ) ; } //初始化程序中使用到的组件 private void InitializeComponent ( ) { this.components = new System.ComponentModel.Container ( ) ; this.button1 = new Button ( ) ; this.listBox1 = new ListBox ( ) ; button1.Location = new System.Drawing.Point ( 16 , 320 ) ; button1.Size = new System.Drawing.Size ( 90 , 23 ) ; button1.TabIndex = 0 ; button1.Text = "读取注册表" ; button1.Click += new System.EventHandler ( this.button1_Click ) ; this.button2 = new Button ( ) ; button2.Location = new System.Drawing.Point ( 116 , 320 ) ; button2.Size = new System.Drawing.Size ( 90 , 23 ) ; button2.TabIndex = 1 ; button2.Text = "创建子键" ; button2.Click += new System.EventHandler ( this.button2_Click ) ; this.button3 = new Button ( ) ; button3.Location = new System.Drawing.Point ( 216 , 320 ) ; button3.Size = new System.Drawing.Size ( 90 , 23 ) ; button3.TabIndex = 2 ; button3.Text = "创建主键" ; button3.Click += new System.EventHandler ( this.button3_Click ) ; this.button4 = new Button ( ) ; button4.Location = new System.Drawing.Point ( 316 , 320 ) ; button4.Size = new System.Drawing.Size ( 90 , 23 ) ; button4.TabIndex = 3 ; button4.Text = "重命名键值" ; button4.Click += new System.EventHandler ( this.button4_Click ) ; listBox1.Location = new System.Drawing.Point ( 16 , 32 ) ; listBox1.Size = new System.Drawing.Size ( 496 , 264 ) ; listBox1.TabIndex = 4 ; this.Text = "用Visual C#来创建和修改注册表中的注册信息~" ; this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ; this.ClientSize = new System.Drawing.Size ( 528 , 357 ) ; //在窗体中加入组件 this.Controls.Add ( this.listBox1 ) ; this.Controls.Add ( this.button1 ) ; this.Controls.Add ( this.button2 ) ; this.Controls.Add ( this.button3 ) ; this.Controls.Add ( this.button4 ) ; } //以列表形式显示"HARDWARE"下面一层的子键和键值 protected void button1_Click ( object sender , System.EventArgs e ) { listBox1.Items.Clear ( ) ; RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "HARDWARE" ) ; //打开"SYSTEM"子键 foreach ( string site in software.GetSubKeyNames ( ) ) //开始遍历由子键名称组成的字符串数组 { listBox1.Items.Add ( site ) ; //在列表中加入子键名称 RegistryKey sitekey = software.OpenSubKey ( site ) ; //打开此子键 foreach ( string sValName in sitekey.GetValueNames ( ) ) //开始遍历由指定子键拥有的键值名称组成的字符串数组 { listBox1.Items.Add ( " " + sValName + ": " + sitekey.GetValue ( sValName ) ) ; //在列表中加入键名称和对应的键值 } } } //创建子键和键值 protected void button2_Click ( object sender , System.EventArgs e ) { listBox1.Items.Clear ( ) ; RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ; RegistryKey ddd = software.CreateSubKey ( "ddd" ) ; ddd.SetValue ( "www" , "1234" ); } //创建一个主键并创建一个键值 protected void button3_Click ( object sender , System.EventArgs e ) { listBox1.Items.Clear ( ) ; RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ; RegistryKey main1 = software.CreateSubKey ( "main" ) ; RegistryKey ddd = main1.CreateSubKey ( "sub" ) ; ddd.SetValue ( "value" , "1234" ) ; } //重命名一个存在的键值 protected void button4_Click ( object sender , System.EventArgs e ) { listBox1.Items.Clear ( ) ; RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ; RegistryKey dddw = software.OpenSubKey ( "aaa" , true ) ; dddw.SetValue ( "bbb" , "abcd" ) ; } public static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } } 1 回答者: qsc80052 如何判断注册表的某个子键是否存在, private void Form1_Load(object sender, EventArgs e) { //检测程序是否设为开机启动 Register rg = new Register(); rg.start_with_windows(); } class Register { /// /// 开机启动注册表检查 /// public void start_with_windows() { RegistryKey hklm = Registry.LocalMachine; RegistryKey run = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); //未设置开机启动 if (run.GetValue("OA邮件提示") == null) { register(); } } /// /// 将程序的开机启动写入注册表 /// private void register() { string starupPath = Application.ExecutablePath; //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装. RegistryKey loca = Registry.LocalMachine; RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); try { run.SetValue("OA邮件提示", starupPath); MessageBox.Show("注册表添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); loca.Close(); run.Close(); } catch (Exception ee) { MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } 连接数据库问题: C# winform程序设置开机启动,当读取配置文件,或者加载图片如 果设置的是相对路径时,开机启动时会出现问题(直接运程程序是没 问题的)。这是因为开机启动的程序要使用绝对路径,相对路径不行。 我们可以通过Application .StartupPath属性经过处理得到文件的绝 对路径问题就解决了。 C# windows服务 自动启动时连数据库出错 悬赏分:0 - 解决时间: 2010年06月26日 20时21分 我做了一个windows服务是自动启动的,但开机启动时总是连不上数据库 开机后手动启动就不会用问题。 提问者: 纯真点点 - 中级魔法师 六级 发消息 加为好友 最佳答案 你设定服务的依赖项,指明该服务依赖MSSQL,就应该能自动启动成功了。 例: 服务项目里的Installer类里加入System.ServiceProcess.ServiceInstaller组件和ServiceProcessInst aller组件, private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; private System.ServiceProcess.ServiceInstaller serviceInstaller1; 然后写InitializeComponent()方法 private void InitializeComponent() { this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; this.serviceProcessInstaller1.Password = null; this.serviceProcessInstaller1.Username = null; this.serviceInstaller1.ServiceName = "YourServiceName"; this.serviceInstaller1.ServicesDependedOn = new string[] {"MSSQLSERVER"}; this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic; this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.serviceProcessInstaller 1, this.serviceInstaller1}); } 2010-06-24 注册表操作 文章分类:.net编程 注册表操作 C#代码 1. //写入启动项 2. private void btnStart_Click(object sender, EventArgs e) 3. { 4. //获取程序执行路径.. 5. string starupPath = Application.ExecutablePath; 6. //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注 册表装. 7. RegistryKey loca = Registry.LocalMachine; 8. RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\Curre ntVersion\Run"); 9. 10. try 11. { 12. //SetValue:存储值的名称 13. run.SetValue("kaixin", starupPath); 14. MessageBox.Show(run.GetValue("kaixin").ToString()); 15. MessageBox.Show("已启用开机运行!", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); 16. loca.Close(); 17. } 18. catch (Exception ee) 19. { 20. MessageBox.Show(ee.Message.ToString(), "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Error); 21. } 22. regset();//获取注册表状态 23. } 24. 25. //删除启动项 26. private void btnStop_Click(object sender, EventArgs e) 27. { 28. //获取程序执行路径.. 29. string starupPath = Application.ExecutablePath; 30. //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注 册表装. 31. RegistryKey loca = Registry.LocalMachine; 32. RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\Curre ntVersion\Run"); 33. 34. try 35. { 36. //SetValue:存储值的名称 37. run.DeleteValue("kaixin"); 38. MessageBox.Show("已停止开机运行!", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); 39. loca.Close(); 40. } 41. catch (Exception ee) 42. { 43. MessageBox.Show(ee.Message.ToString(), "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Error); 44. } 45. regset();//获取注册表状态 46. } 47. 48. //状态 49. private void regset() 50. { 51. //获取程序执行路径.. 52. // string starupPath = Application.ExecutablePath; 53. //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注 册表装. 54. RegistryKey loca = Registry.LocalMachine; 55. RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\Curre ntVersion\Run"); 56. string[] subkeyNames; 57. subkeyNames = run.GetValueNames(); 58. foreach (string keyName in subkeyNames) 59. { 60. if (keyName == "kaixin") 61. { 62. this.lblMsg.Text = "开机运行(启动中)"; 63. this.btnStart.Enabled = false; 64. this.btnStop.Enabled = true; 65. } 66. else 67. { 68. this.lblMsg.Text = "开机运行(已停止)"; 69. this.btnStart.Enabled = true; 70. this.btnStop.Enabled = false; 71. } 72. } 73. } C#代码 1. /// 2. /// 根据指定注册表路径启动程序 3. /// 4. private static void GetPathStart() 5. { 6. RegistryKey loca = Registry.LocalMachine; 7. //路径可自定义 8. RegistryKey GetPath = loca.CreateSubKey(@"SOFTWARE\Microsoft\VisualStu dio\9.0\Setup"); 9. string Path = GetPath.GetValue("Dbghelp_path").ToString(); 10. Process.Start(Path + @"\devenv.exe"); 11. }
本文档为【C#设置开机自动启动.doc】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_079973
暂无简介~
格式:doc
大小:50KB
软件:Word
页数:0
分类:生活休闲
上传时间:2018-09-09
浏览量:10