using Microsoft.Win32;
#region 设置程序开机运行
// <summary>
/// 设置程序开机运行
/// </summary>
/// <param name="fileName">要运行的EXE程序名称</param>
/// <param name="isAutoRun">是否开机运行</param>
private 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
{
throw; // new Exception(ex.ToString());
}
finally
{
if (reg != null)
reg.Close();
}
}
#endregion
C# 设置Winform开机运行
最新推荐文章于 2020-12-06 22:46:14 发布