首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请问C# 写注册表 随系统启动有关问题

2013-04-07 
请教C# 写注册表 随系统启动问题 /// summary/// 注册表操作,将程序添加到启动项/// /summarytry{Micr

请教C# 写注册表 随系统启动问题
 

/// <summary>
                /// 注册表操作,将程序添加到启动项
                /// </summary>
                try
                {
                    Microsoft.Win32.RegistryKey Reg;
                    string ShortFileName = Application.ProductName;
                    Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    if (Reg == null)
                    {
                        Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
                    }
                    Reg.SetValue(ShortFileName, Application.ExecutablePath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }



//////////取消启动
 string ShortFileName = Application.ProductName;           //获得应用程序名

                try
                {
                    Microsoft.Win32.RegistryKey Reg;
                    Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    if (Reg == null)
                    {
                        Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
                    }


                    Reg.DeleteValue(ShortFileName, false);
                }
                catch (Exception)
                {
                    return;
                }





电脑启动时有错误提示,运行不了,但是直接双击竟然是可以正常启动软件。
[解决办法]
检查你的程序执行上述代码后,注册表中是否有这么一个记录,指向的程序路径、文件名是否正确
目测Reg.SetValue(ShortFileName, Application.ExecutablePath);这里不对,应该包含exe文件名后面。
[解决办法]
可能是程序权限、以及程序中使用了外部文件但是相对路径没有匹配的问题。
[解决办法]
程序有没有依赖的启动项,
比如SQL服务管理器还没有启动,
你的程序已经开始连接数据库了
提示连接失败的时候,
你的服务管理器还没有启动

热点排行