/// <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();
}
}
/// 设置应用程序开机自动运行
/// </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);
失误,应该是name
那么else下面该这样写 reg.DeleteValue(name, false);
reg.Close();最好colse一下。
http://www.geekpedia.com/tutorial151_Run-the-application-at-Windows-startup.html
你注册表操作的不对,仔细看看我发给你的网站如果只想对当前用户有效,加在这里
HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run 如果想对所有用户都有效,则加在这里
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run.
开机启动之后!
报错if (!System.IO.File.Exists(fileName))
throw new Exception("该文件不存在!");
开机启动后就会有这个问题,找不到文件!
重新运行下就可以!
又纠结了
连接语句后面加个超时。
我用的timout=10 改一下就可以吗?
大哥你的代码!跟我的差不多!/// <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();
}
}
{
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”即可
是这样写的!但是还是连接数据库失败
错误提示:“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();
}
}
写入注册表也行。 这些可以用程序完成。控制文件。 另外你可以写一个 window 服务辅助程序。 系统开机将会执行.