/// <summary>
        /// 保存开启启动设置的方法
        /// </summary>
        private void DoAutoRunXML()
        {
            XmlNode root = clsXMl.GetRootNode();
            foreach (XmlNode node in root.ChildNodes)
            {
                if (node.Name == "AutoRun")
                {
                    if (node.InnerText == "no")
                    {
                        node.InnerText = "yes";//重写XML
                        ToolStripMenuItemAutoRun.Checked = true;
                        RunWhenStart(true, Application.ProductName, Application.StartupPath + "\\" + Application.ProductName + ".exe");
                    }
                    else if (node.InnerText == "yes")
                    {
                        node.InnerText = "no";
                        ToolStripMenuItemAutoRun.Checked = false;
                        RunWhenStart(false, Application.ProductName, Application.StartupPath + "\\" + Application.ProductName + ".exe");

                    }
                }
            }

            clsXMl.xmlSave();
        }

        /// <summary>
        /// 修改 开机启动 注册表的方法
        /// </summary>
        /// <param name="bFlag"> 是否开机启动</param>
        /// <param name="strName">启动值的名称</param>
        /// <param name="strPath">启动程序的路径</param>
        private void RunWhenStart(bool bFlag, string strName, string strPath)
        {
            Microsoft.Win32.RegistryKey rootKey = Microsoft.Win32.Registry.LocalMachine;//本地计算机数据的配置
            Microsoft.Win32.RegistryKey runKey = rootKey.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

            if (bFlag == true)//创建开机启动
            {
                try
                {
                    runKey.SetValue(strName, strPath);
                    rootKey.Close();// 刷新 关闭 保存修改
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, " 提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            else
            {
                try
                {
                    runKey.DeleteValue(strName);
                    rootKey.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, " 提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            GC.Collect();
        }

 

 

 

 

 

 

 

 

posted on 2010-10-09 09:22  freedom831215  阅读(215)  评论(0编辑  收藏  举报