/// <summary>
        /// 设置应用程序开机自动运行
        /// </summary>
        /// <param name="fileName">应用程序的文件名</param>
        /// <param name="isAutoRun">是否自动运行,为false时,取消自动运行</param>
        /// <exception cref="System.Exception">设置不成功时抛出异常</exception>
         public static void SetAutoRun(string fileName,bool isAutoRun) 
        {
            RegistryKey reg=null;
            try
            {
                if (!System.IO.File.Exists(fileName))
                    throw new Exception("该文件不存在!");
                String name = fileName.Substring(fileName.LastIndexOf("\\") + 1);
                reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (reg == null)
                    reg = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                if (isAutoRun)
                    reg.SetValue(name, fileName);
                else
                    reg.SetValue(name, false);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
                finally
            {
                if(reg!=null)
                reg.Close();
            }        
        }

解决方案 »

  1.   

    网上找个instsrv.exe,srvany.exe比自己写注册表好用吧。
      

  2.   

    你的那个方法有错,reg.setvalue(名称,路径)
      

  3.   

            /// <summary>
            /// 设置应用程序开机自动运行
            /// </summary>
            /// <param name="fileName">应用程序的文件名</param>
            /// <param name="isAutoRun">是否自动运行,为false时,取消自动运行</param>
            /// <exception cref="System.Exception">设置不成功时抛出异常</exception>
            public static void SetAutoRun(string fileName, bool isAutoRun)
            {
                RegistryKey reg = null;
                try
                {
                    if (!System.IO.File.Exists(fileName))
                        throw new Exception("该文件不存在!");
                    String name = fileName.Substring(fileName.LastIndexOf("\\") + 1);
                    reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    reg.OpenSubKey(fileName);
                    if (reg == null)
                        reg = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                    if (isAutoRun)
                        reg.SetValue(name, fileName);
                    else
                        reg.SetValue(name, false);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
                finally
                {
                    if (reg != null)
                        reg.Close();
                }
            }
            //调用
              SetAutoRun("flyfox.exe", true); 
      

  4.   

    reg.OpenSubKey(fileName);
    失误,应该是name
      

  5.   

    我想你传boolen类型,是为了true增加,false删除吧
    那么else下面该这样写                  reg.DeleteValue(name, false);
                    reg.Close();最好colse一下。
      

  6.   

    把编译出来的exe的快捷方式 放到 开始-》程序-》启动中 就ok了
      

  7.   

    看看这个
    http://www.geekpedia.com/tutorial151_Run-the-application-at-Windows-startup.html
      

  8.   


    你注册表操作的不对,仔细看看我发给你的网站如果只想对当前用户有效,加在这里
     HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run 如果想对所有用户都有效,则加在这里
    HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run.
      

  9.   

    是不是你要启动的程序有问题,你换成notepad看看能否正常运行起来
      

  10.   

    就问题解决了!新问题又有了!
    开机启动之后!
    报错if (!System.IO.File.Exists(fileName))
        throw new Exception("该文件不存在!");
    开机启动后就会有这个问题,找不到文件!
    重新运行下就可以!
    又纠结了
      

  11.   

    SetAutoRun("flyfox.exe", true);这个flyfox.exe要完整路径名
      

  12.   

    恩,这个问题常见很,因为服务的启动顺序不一致,可能你的exe比数据库启动的快,导致连不到数据库,远程数据库就不会有问题了。
      

  13.   

    你的程序可以启动一个timer,然后定时去Ping数据库,然后对上述情况进行处理
      

  14.   

    connection timeout=30
    连接语句后面加个超时。
      

  15.   

    建议你换个方式,设置为服务启动,服务启动是有启动顺序的,设置服务依赖项为SQLSERVER服务,那么这个服务必然会等SQLSERVER开启后才去执行,你再在服务中调用你实际的程序(如果没有界面,仅服务,直接放在里面跑就可以)。
      

  16.   


    我用的timout=10 改一下就可以吗?
      

  17.   

    可以把你的.exe注册成win服务,开机自动启动就可以了,注册服务用批处理,百度一下了
      

  18.   

    默认是15秒,我的意思是大一点看行不,还有,实在不行,你做一个查看服务的方法,看数据库server起来没,起来了再连接,引用System.ServiceProcess.ServiceController,这样很不科学。
      

  19.   

    也不知道搜索下,http://www.cnblogs.com/jinyuttt/archive/2010/07/16/1778837.html仅供参考。会c++就应该看得懂,不懂问我。
      

  20.   


    大哥你的代码!跟我的差不多!/// <summary>
            /// 设置应用程序开机自动运行
            /// </summary>
            /// <param name="fileName">应用程序的文件名</param>
            /// <param name="isAutoRun">是否自动运行,为false时,取消自动运行</param>
            /// <exception cref="System.Exception">设置不成功时抛出异常</exception>
            public static void SetAutoRun(string fileName, bool isAutoRun)
            {
                RegistryKey reg = null;
                try
                {
                    
                    if (!System.IO.File.Exists(fileName))
                        throw new Exception("该文件不存在!");
                    String name = fileName.Substring(fileName.LastIndexOf("\\") + 1);
                    reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    reg.OpenSubKey(name);
                    if (reg == null)
                        reg = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                    if (isAutoRun)
                        reg.SetValue(name,fileName);
                    else
                    {
                        reg.SetValue(name, false);
                        reg.Close();
                    }
                        
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
                finally
                {
                    if (reg != null)
                        reg.Close();
                }
            }
      

  21.   

    这是服务类的基本写法,如果你不明示你写的是啥程序,我只能给你个架构,内部自己考虑怎么写。    partial class Service1 : ServiceBase
        {
            public Service1()
            {        }        protected override void OnStart(string[] args)
            {        }        protected override void OnStop()
            {
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new Service1() 
                };
                ServiceBase.Run(ServicesToRun);
            }
        }
    之后注册服务用批处理命令“sc”即可
      

  22.   

    写 windows services在services做监控启动服务的时候 启动程序 
      

  23.   

    windows服务,由好几块程序组成,C#6版上有例子。
      

  24.   


    是这样写的!但是还是连接数据库失败
    错误提示:“System.Exception: MyError:无法打开登录所请求的数据库 "zmgas"。登录失败。
    用户 'sa' 登录失败。”
     partial class MonitorService : ServiceBase
        {
            private GasConfig config = new GasConfig();        private string path = "";
            public MonitorService()
            {
                InitializeComponent();
            }        protected override void OnStart(string[] args)
            {          
                startFileMonitor();//启动程序
                startReadFileData();            this.timer1.Enabled = true;
                this.timer1.Start();   
            }
       }
      

  25.   

     
    写入注册表也行。  这些可以用程序完成。控制文件。  另外你可以写一个 window 服务辅助程序。 系统开机将会执行.