您的位置:首页 > 编程语言 > C#

C# 实现是否设置指定程序为开机启动程序

2016-07-08 09:35 519 查看
/// <summary>

        /// 是否设置程序为开机启动界面

        /// </summary>

        /// <param name="appPath">程序启动路径</param>

        /// <param name="nodelist">true为设置为启动桌面,false为重新设置为操作系统界面</param>

        /// <returns>修改成功返回true</returns> 

        public bool IsSetDesktop(string appPath, bool isSetDesktop)

        {

            bool ret = false;

            RegistryKey reg = null;

            try

            {

                reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System", false);

                if (isSetDesktop)

                {

                    if (null == reg)

                    {

                        reg = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System");

                    }

                    reg.SetValue("Shell", appPath);

                    ret = true;

                }

                else

                {

                    reg.SetValue("Shell", "explorer.exe");//explorer.exe为操作系统显示桌面的exe程序

                }

            }

            catch (Exception e)

            {

                Log.Exception("Set System Desktop error:" + e.Message);

            }

            finally

            {

                if (null != reg)

                {

                    reg.Close();

                }

            }

            return ret;

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: