第一種方式 修改注冊表
/// <summary>
/// 開機啟動
/// </summary>
public void OpenStart()
{
//獲得程序路徑
var starupPath = GetType().Assembly.Location;
try
{
var fileName = starupPath;
//設(shè)置啟動項的key 可以改
var shortFileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
//打開子鍵節(jié)點
var myReg = Registry.LocalMachine.OpenSubKey(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", RegistryKeyPermissionCheck.ReadWriteSubTree,
RegistryRights.FullControl);
if (myReg == null)
{
//如果子鍵節(jié)點不存在,則創(chuàng)建之
myReg = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
if (myReg != null && myReg.GetValue(shortFileName) != null)
{
//在注冊表中設(shè)置自啟動程序
myReg.DeleteValue(shortFileName);
myReg.SetValue(shortFileName, fileName);
}
else if (myReg != null && myReg.GetValue(shortFileName) == null)
{
//設(shè)置啟動項的key 和啟動路徑*******
myReg.SetValue(shortFileName, fileName);
}
}
catch
{
}
}
/// <summary>
/// 判斷注冊鍵值對是否存在,即是否處于開機啟動狀態(tài)
/// </summary>
/// <param name="keyName">鍵值名</param>
/// <returns></returns>
private static bool IsExistKey(string keyName)
{
try
{
bool _exist = false;
RegistryKey local = Registry.LocalMachine;
RegistryKey runs = local.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (runs == null)
{
RegistryKey key2 = local.CreateSubKey("SOFTWARE");
RegistryKey key3 = key2.CreateSubKey("Microsoft");
RegistryKey key4 = key3.CreateSubKey("Windows");
RegistryKey key5 = key4.CreateSubKey("CurrentVersion");
RegistryKey key6 = key5.CreateSubKey("Run");
runs = key6;
}
string[] runsName = runs.GetValueNames();
foreach (string strName in runsName)
{
if (strName.ToUpper() == keyName.ToUpper())
{
_exist = true;
return _exist;
}
}
return _exist;
}
catch
{
return false;
}
}
/// <summary>
/// 取消開機啟動
/// </summary>
/// <param name="key"></param>
private void DelStart(string key)
{
try
{
string RegeditPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\";
RegistryKey rgk = Registry.LocalMachine
.OpenSubKey(RegeditPath, true)
.OpenSubKey("Run", true);
string[] str = rgk.GetValueNames();
rgk.DeleteValue(key);
rgk.Close();
}
catch (Exception ex)
{
}
}
第二種添加快捷方式到啟動菜單中文章來源:http://www.zghlxwxcb.cn/news/detail-771521.html
添加引用,在 Com 中搜索 Windows Script Host Object Mod文章來源地址http://www.zghlxwxcb.cn/news/detail-771521.html

using IWshRuntimeLibrary; //添加引用,在 Com 中搜索 Windows Script Host Object Model
using System.Diagnostics;
/// <summary>
/// 快捷方式名稱-任意自定義
/// </summary>
private const string QuickName = "WindowsClient";
/// <summary>
/// 自動獲取系統(tǒng)自動啟動目錄
/// </summary>
private string systemStartPath { get { return Environment.GetFolderPath(Environment.SpecialFolder.Startup); } }
/// <summary>
/// 自動獲取程序完整路徑
/// </summary>
private string appAllPath { get { return Process.GetCurrentProcess().MainModule.FileName; } }
/// <summary>
/// 自動獲取桌面目錄
/// </summary>
private string desktopPath { get { return Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); } }
/// <summary>
/// 設(shè)置開機自動啟動-只需要調(diào)用改方法就可以了參數(shù)里面的bool變量是控制開機啟動的開關(guān)的,默認(rèn)為開啟自啟啟動
/// </summary>
/// <param name="onOff">自啟開關(guān)</param>
/// <param name="appPath">要設(shè)置自動啟動的路徑</param>
public void SetMeAutoStart(bool onOff = true, string appPath = null)
{
if (onOff)//開機啟動
{
//獲取啟動路徑應(yīng)用程序快捷方式的路徑集合
List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appPath != null ? appPath : appAllPath);
//存在2個以快捷方式則保留一個快捷方式-避免重復(fù)多于
if (shortcutPaths.Count >= 2)
{
for (int i = 1; i < shortcutPaths.Count; i++)
{
DeleteFile(shortcutPaths[i]);
}
}
else if (shortcutPaths.Count < 1)//不存在則創(chuàng)建快捷方式
{
CreateShortcut(systemStartPath, QuickName, appPath != null ? appPath : appAllPath, "SAGA");
}
}
else//開機不啟動
{
//獲取啟動路徑應(yīng)用程序快捷方式的路徑集合
List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appPath != null ? appPath : appAllPath);
//存在快捷方式則遍歷全部刪除
if (shortcutPaths.Count > 0)
{
for (int i = 0; i < shortcutPaths.Count; i++)
{
DeleteFile(shortcutPaths[i]);
}
}
}
//創(chuàng)建桌面快捷方式-如果需要可以取消注釋
//CreateDesktopQuick(desktopPath, QuickName, appAllPath);
}
/// <summary>
/// 向目標(biāo)路徑創(chuàng)建指定文件的快捷方式
/// </summary>
/// <param name="directory">目標(biāo)目錄</param>
/// <param name="shortcutName">快捷方式名字</param>
/// <param name="targetPath">文件完全路徑</param>
/// <param name="description">描述</param>
/// <param name="iconLocation">圖標(biāo)地址</param>
/// <returns>成功或失敗</returns>
private bool CreateShortcut(string directory, string shortcutName, string targetPath, string description = null, string iconLocation = null)
{
try
{
//啟動項菜單
//C:\Users\lenovo\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); //目錄不存在則創(chuàng)建
//添加引用 Com 中搜索 Windows Script Host Object Model
string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName)); //合成路徑
WshShell shell = new IWshRuntimeLibrary.WshShell();
IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); //創(chuàng)建快捷方式對象
shortcut.TargetPath = targetPath; //指定目標(biāo)路徑
shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); //設(shè)置起始位置
shortcut.WindowStyle = 1; //設(shè)置運行方式,默認(rèn)為常規(guī)窗口
shortcut.Description = description; //設(shè)置備注
shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation; //設(shè)置圖標(biāo)路徑
shortcut.Save(); //保存快捷方式
return true;
}
catch (Exception ex)
{
string temp = ex.Message;
temp = "";
}
return false;
}
/// <summary>
/// 獲取指定文件夾下指定應(yīng)用程序的快捷方式路徑集合
/// </summary>
/// <param name="directory">文件夾</param>
/// <param name="targetPath">目標(biāo)應(yīng)用程序路徑</param>
/// <returns>目標(biāo)應(yīng)用程序的快捷方式</returns>
private List<string> GetQuickFromFolder(string directory, string targetPath)
{
List<string> tempStrs = new List<string>();
tempStrs.Clear();
string tempStr = null;
string[] files = Directory.GetFiles(directory, "*.lnk");
if (files == null || files.Length < 1)
{
return tempStrs;
}
for (int i = 0; i < files.Length; i++)
{
//files[i] = string.Format("{0}\\{1}", directory, files[i]);
tempStr = GetAppPathFromQuick(files[i]);
if (tempStr == targetPath)
{
tempStrs.Add(files[i]);
}
}
return tempStrs;
}
/// <summary>
/// 獲取快捷方式的目標(biāo)文件路徑-用于判斷是否已經(jīng)開啟了自動啟動
/// </summary>
/// <param name="shortcutPath"></param>
/// <returns></returns>
private string GetAppPathFromQuick(string shortcutPath)
{
//快捷方式文件的路徑 = @"d:\Test.lnk";
if (System.IO.File.Exists(shortcutPath))
{
WshShell shell = new WshShell();
IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortcutPath);
//快捷方式文件指向的路徑.Text = 當(dāng)前快捷方式文件IWshShortcut類.TargetPath;
//快捷方式文件指向的目標(biāo)目錄.Text = 當(dāng)前快捷方式文件IWshShortcut類.WorkingDirectory;
return shortct.TargetPath;
}
else
{
return "";
}
}
/// <summary>
/// 根據(jù)路徑刪除文件-用于取消自啟時從計算機自啟目錄刪除程序的快捷方式
/// </summary>
/// <param name="path">路徑</param>
private void DeleteFile(string path)
{
FileAttributes attr = System.IO.File.GetAttributes(path);
if (attr == FileAttributes.Directory)
{
Directory.Delete(path, true);
}
else
{
System.IO.File.Delete(path);
}
}
到了這里,關(guān)于C#程序開機自啟的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!