
/**//// <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();

}



}