http://www.diybl.com/course/4_webprogram/asp.net/netjs/20090309/160291.html#
C# winform程序设置开机启动,当读取配置文件,或者加载图片如果设置的是相对路径时,开机启动时会出现问题(直接运程程序是没问题的)。这是因为开机 启动的程序要使用绝对路径,相对路径不行。我们可以通过Application .StartupPath属性经过处理得到文件的绝对路径问题就解决了。
C# 通过读写注册表来设置开机启动想方法很简单,网上很多:
/// <summary> /// 开机启动项 /// </summary> /// <param name="Started">是否启动</param> /// <param name="name">启动值的名称</param> /// <param name="path">启动程序的路径</param> public void RunWhenStart( bool Started, string name, string path) { RegistryKey HKLM = Registry.LocalMachine; RegistryKey Run = HKLM.CreateSubKey(@ "SOFTWARE/Microsoft/Windows/CurrentVersion/Run" ); if (Started == true ) { try { Run.SetValue(name, path); HKLM.Close(); } catch //没有权限会异常 {} } else { try { Run.DeleteValue(name); HKLM.Close(); } catch //没有权限会异常 {} } }
或者直接:
//添加启动 RegistryKey ms_run = Registry.LocalMachine.OpenSubKey( "SOFTWARE//Microsoft//Windows//CurrentVersion//Run" , true ); ms_run.SetValue( "mistysoft" , Application.ExecutablePath.ToString()); //删除启动(设为控,注册表项还在) RegistryKey ms_run = Registry.LocalMachine.OpenSubKey( "SOFTWARE//Microsoft//Windows//CurrentVersion//Run" , true ); ms_run.SetValue( "mistysoft" , "" );