开机自动运行的方法


using Microsoft.Win32;//Registry的引用空间.

private void FrmTiming_Load(object sender, EventArgs e)
{
AutoRun();
}


/// <summary>
/// windows启动时,自动运行
/// </summary>
private void AutoRun()
{
try
{
string path = Application.ExecutablePath;
if (!System.IO.File.Exists(path))
return;
string myName = path.Substring(path.LastIndexOf("""") + 1);
RegistryKey myReg
= Registry.LocalMachine.OpenSubKey("SOFTWARE""Microsoft""Windows""CurrentVersion""Run", true);
if (myReg == null)
myReg
= Registry.LocalMachine.CreateSubKey("SOFTWARE""Microsoft""Windows""CurrentVersion""Run");
myReg.SetValue(myName, path);
}
catch
{
Application.Exit();
}

}

 

posted on 2008-08-23 18:00  冷月孤峰  阅读(235)  评论(0)    收藏  举报