Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 4k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using Microsoft.Win32;
  8. using System.IO;
  9. namespace ch4_5
  10. {
  11. /// <summary>
  12. /// Form1 的摘要说明。
  13. /// </summary>
  14. public class Form1 : System.Windows.Forms.Form
  15. {
  16. private System.Windows.Forms.Button button1;
  17. private System.Windows.Forms.Label label1;
  18. private System.Windows.Forms.Button button2;
  19. /// <summary>
  20. /// 必需的设计器变量。
  21. /// </summary>
  22. private System.ComponentModel.Container components = null;
  23. public Form1()
  24. {
  25. //
  26. // Windows 窗体设计器支持所必需的
  27. //
  28. InitializeComponent();
  29. //
  30. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  31. //
  32. }
  33. /// <summary>
  34. /// 清理所有正在使用的资源。
  35. /// </summary>
  36. protected override void Dispose( bool disposing )
  37. {
  38. if( disposing )
  39. {
  40. if (components != null) 
  41. {
  42. components.Dispose();
  43. }
  44. }
  45. base.Dispose( disposing );
  46. }
  47. #region Windows Form Designer generated code
  48. /// <summary>
  49. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  50. /// 此方法的内容。
  51. /// </summary>
  52. private void InitializeComponent()
  53. {
  54. this.button1 = new System.Windows.Forms.Button();
  55. this.label1 = new System.Windows.Forms.Label();
  56. this.button2 = new System.Windows.Forms.Button();
  57. this.SuspendLayout();
  58. // 
  59. // button1
  60. // 
  61. this.button1.Location = new System.Drawing.Point(24, 128);
  62. this.button1.Name = "button1";
  63. this.button1.Size = new System.Drawing.Size(96, 23);
  64. this.button1.TabIndex = 0;
  65. this.button1.Text = "确定";
  66. this.button1.Click += new System.EventHandler(this.button1_Click);
  67. // 
  68. // label1
  69. // 
  70. this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  71. this.label1.Location = new System.Drawing.Point(24, 40);
  72. this.label1.Name = "label1";
  73. this.label1.Size = new System.Drawing.Size(240, 64);
  74. this.label1.TabIndex = 1;
  75. this.label1.Text = "你想开机就自动启动这个窗体?";
  76. this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  77. // 
  78. // button2
  79. // 
  80. this.button2.Location = new System.Drawing.Point(144, 128);
  81. this.button2.Name = "button2";
  82. this.button2.Size = new System.Drawing.Size(104, 23);
  83. this.button2.TabIndex = 2;
  84. this.button2.Text = "取消";
  85. this.button2.Click += new System.EventHandler(this.button2_Click);
  86. // 
  87. // Form1
  88. // 
  89. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  90. this.ClientSize = new System.Drawing.Size(292, 213);
  91. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  92.   this.button2,
  93.   this.label1,
  94.   this.button1});
  95. this.Name = "Form1";
  96. this.Text = "自动启动的窗体";
  97. this.ResumeLayout(false);
  98. }
  99. #endregion
  100. /// <summary>
  101. /// 应用程序的主入口点。
  102. /// </summary>
  103. [STAThread]
  104. static void Main() 
  105. {
  106. Application.Run(new Form1());
  107. }
  108. private void button1_Click(object sender, System.EventArgs e)
  109. {
  110.     //如果确定将启动信息写入注册表
  111. if (MessageBox.Show("你确定开机就要启动这个程序吗?","提示",MessageBoxButtons.OKCancel)==
  112. DialogResult.OK)
  113. {
  114. //获得文件的当前路径
  115. string dir=Directory.GetCurrentDirectory();
  116. //获取可执行文件的全部路径
  117. string exeDir=dir+"\ch4_5.exe";
  118. //获取Run键
  119. RegistryKey key1=Registry.LocalMachine;
  120. RegistryKey key2=key1.CreateSubKey("SOFTWARE");
  121. RegistryKey key3=key2.CreateSubKey("Microsoft");
  122. RegistryKey key4=key3.CreateSubKey("Windows");
  123. RegistryKey key5=key4.CreateSubKey("CurrentVersion");
  124. RegistryKey key6=key5.CreateSubKey("Run");
  125. //在Run键中写入一个新的键值
  126. key6.SetValue("myForm",exeDir);
  127.        }
  128. }
  129. private void button2_Click(object sender, System.EventArgs e)
  130. {
  131. //如果确定将启动信息删除
  132. if (MessageBox.Show("你确定要删除注册表中的信息吗?","提示",MessageBoxButtons.OKCancel)==
  133. DialogResult.OK)
  134. {
  135.    
  136. //获取Run键
  137. RegistryKey key1=Registry.LocalMachine;
  138. RegistryKey key2=key1.CreateSubKey("SOFTWARE");
  139. RegistryKey key3=key2.CreateSubKey("Microsoft");
  140. RegistryKey key4=key3.CreateSubKey("Windows");
  141. RegistryKey key5=key4.CreateSubKey("CurrentVersion");
  142. RegistryKey key6=key5.CreateSubKey("Run");
  143. //删除键值
  144. key6.DeleteValue("myForm",false);
  145. }
  146. }
  147. }
  148. }