昨天想到把一个程序做成在windows启动后自动启动,之前都没有接触过在C sharp中控制注册表,搜索一下看了些资料,大概东西都清楚了,自己做了一个,
Code
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using Microsoft.Win32;
9
10/**////<summary>
11/// jospeh, 071212
12/// set this app in Registry:
13/// HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
14///</summary>
15namespace AutoRun
16{
17 public partial class frmMain : Form
18 {
19 public const string strAutoKey = @"HKEY_CURRENT_USER";
20 public const string strSubKey = @"\Software\Microsoft\Windows\CurrentVersion\Run";
21 public const char chrSeparator = '\\';
22 public const string strKeyName = "TestAutoRun";
23
24 public frmMain()
25 {
26 InitializeComponent();
27 }
28
29 //set this app on autorun
30 private void btnOn_Click(object sender, EventArgs e)
31 {
32 try
33 {
34 //set the key value
35 Registry.SetValue(strAutoKey+strSubKey, strKeyName, Environment.CurrentDirectory + chrSeparator + "AutoRun.exe");
36 MessageBox.Show("Now this application is autorun with windows starts up.");
37 }
38 catch (Exception eOn)
39 {
40 MessageBox.Show(eOn.Message);
41 }
42 }
43
44 // set this app off autorun
45 private void btnOff_Click(object sender, EventArgs e)
46 {
47 try
48 {
49 //recursion get the last subkey
50 RegistryKey rkRun = GetRunKey (strSubKey,Registry .CurrentUser );
51 //circle in Values belong to key
52 foreach (string strTemp in rkRun.GetValueNames())
53 {
54 //find the key want to be delete
55 if (strTemp == strKeyName)
56 {
57 rkRun.DeleteValue(strTemp);
58 MessageBox.Show("Now this application is not autorun with windows starts up.");
59 break;
60 }
61 }
62
63 }
64 catch (Exception eOff)
65 {
66 MessageBox.Show(eOff.Message);
67 }
68 }
69
70 /**//// <summary>
71 /// joseph, 071212
72 /// get the last sub key with recursion
73 /// </summary>
74 /// <param name="strKeyTree">subkey tree</param>
75 /// <param name="rgkParent">Parent registrykey</param>
76 /// <returns></returns>
77 RegistryKey GetRunKey(string strKeyTree,RegistryKey rgkParent)
78 {
79 RegistryKey rgkChild;
80 string strChild = string.Empty;
81 string strRemain = string.Empty;
82
83 //has one \ char, or not
84 if(strKeyTree .IndexOf (chrSeparator )!=strKeyTree .LastIndexOf (chrSeparator ))
85 {
86 //remove \ in the head
87 strKeyTree = strKeyTree.Substring(1, strKeyTree.Length - 1);
88 //get first sub key name
89 strChild=strKeyTree .Substring (0,strKeyTree.IndexOf (chrSeparator ));
90 //get the first sub key
91 rgkChild = rgkParent.OpenSubKey(strChild);
92 //keep the remain part of strKeyTree except first part
93 strRemain = strKeyTree.Substring(strKeyTree.IndexOf(chrSeparator), strKeyTree.Length - strKeyTree.IndexOf(chrSeparator));
94
95 return GetRunKey(strRemain, rgkChild);
96
97 }
98 else //only the last sub tree key
99 {
100 strKeyTree = strKeyTree.Substring(1, strKeyTree.Length - 1);
101 rgkChild = rgkParent.OpenSubKey(strKeyTree,true);
102 return rgkChild;
103 }
104
105 }
106
107 }
108}
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using Microsoft.Win32;
9
10/**////<summary>
11/// jospeh, 071212
12/// set this app in Registry:
13/// HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
14///</summary>
15namespace AutoRun
16{
17 public partial class frmMain : Form
18 {
19 public const string strAutoKey = @"HKEY_CURRENT_USER";
20 public const string strSubKey = @"\Software\Microsoft\Windows\CurrentVersion\Run";
21 public const char chrSeparator = '\\';
22 public const string strKeyName = "TestAutoRun";
23
24 public frmMain()
25 {
26 InitializeComponent();
27 }
28
29 //set this app on autorun
30 private void btnOn_Click(object sender, EventArgs e)
31 {
32 try
33 {
34 //set the key value
35 Registry.SetValue(strAutoKey+strSubKey, strKeyName, Environment.CurrentDirectory + chrSeparator + "AutoRun.exe");
36 MessageBox.Show("Now this application is autorun with windows starts up.");
37 }
38 catch (Exception eOn)
39 {
40 MessageBox.Show(eOn.Message);
41 }
42 }
43
44 // set this app off autorun
45 private void btnOff_Click(object sender, EventArgs e)
46 {
47 try
48 {
49 //recursion get the last subkey
50 RegistryKey rkRun = GetRunKey (strSubKey,Registry .CurrentUser );
51 //circle in Values belong to key
52 foreach (string strTemp in rkRun.GetValueNames())
53 {
54 //find the key want to be delete
55 if (strTemp == strKeyName)
56 {
57 rkRun.DeleteValue(strTemp);
58 MessageBox.Show("Now this application is not autorun with windows starts up.");
59 break;
60 }
61 }
62
63 }
64 catch (Exception eOff)
65 {
66 MessageBox.Show(eOff.Message);
67 }
68 }
69
70 /**//// <summary>
71 /// joseph, 071212
72 /// get the last sub key with recursion
73 /// </summary>
74 /// <param name="strKeyTree">subkey tree</param>
75 /// <param name="rgkParent">Parent registrykey</param>
76 /// <returns></returns>
77 RegistryKey GetRunKey(string strKeyTree,RegistryKey rgkParent)
78 {
79 RegistryKey rgkChild;
80 string strChild = string.Empty;
81 string strRemain = string.Empty;
82
83 //has one \ char, or not
84 if(strKeyTree .IndexOf (chrSeparator )!=strKeyTree .LastIndexOf (chrSeparator ))
85 {
86 //remove \ in the head
87 strKeyTree = strKeyTree.Substring(1, strKeyTree.Length - 1);
88 //get first sub key name
89 strChild=strKeyTree .Substring (0,strKeyTree.IndexOf (chrSeparator ));
90 //get the first sub key
91 rgkChild = rgkParent.OpenSubKey(strChild);
92 //keep the remain part of strKeyTree except first part
93 strRemain = strKeyTree.Substring(strKeyTree.IndexOf(chrSeparator), strKeyTree.Length - strKeyTree.IndexOf(chrSeparator));
94
95 return GetRunKey(strRemain, rgkChild);
96
97 }
98 else //only the last sub tree key
99 {
100 strKeyTree = strKeyTree.Substring(1, strKeyTree.Length - 1);
101 rgkChild = rgkParent.OpenSubKey(strKeyTree,true);
102 return rgkChild;
103 }
104
105 }
106
107 }
108}