C#實(shí)現(xiàn)軟件開機(jī)自啟動(dòng)功能(不需要管理員權(quán)限)
原理簡(jiǎn)介
本文參考C#實(shí)現(xiàn)軟件開機(jī)自動(dòng)啟動(dòng)的兩種常用方法,將里面中的第一種方法做了封裝成AutoStart類,使用時(shí)直接兩三行代碼就可以搞定。
自啟動(dòng)的原理是將軟件的快捷方式創(chuàng)建到計(jì)算機(jī)的自動(dòng)啟動(dòng)目錄下(不需要管理員權(quán)限) ,這種方法更加通用、限制更少。
使用方法
使用方法如下:
//快捷方式的描述、名稱的默認(rèn)值是當(dāng)前的進(jìn)程名,自啟動(dòng)默認(rèn)為正常窗口,一般情況下不需要手動(dòng)設(shè)置 //設(shè)置快捷方式的描述, AutoStart.Instance.QuickDescribe = "軟件描述"; //設(shè)置快捷方式的名稱 AutoStart.Instance.QuickName = "軟件名稱"; //設(shè)置自啟動(dòng)的窗口類型,后臺(tái)服務(wù)類的軟件可以設(shè)置為最小窗口 AutoStart.Instance.WindowStyle = WshWindowStyle.WshMinimizedFocus; //快捷方式設(shè)置true時(shí),有就忽略、沒有就創(chuàng)建,自啟動(dòng)快捷方式只能存在一個(gè) //設(shè)置開機(jī)自啟動(dòng),true 自啟動(dòng),false 不自啟動(dòng) AutoStart.Instance.SetAutoStart(SysParam.Instance.OnOff); //設(shè)置桌面快捷方式,true 創(chuàng)建桌面快捷方式(有就跳過,沒有就創(chuàng)建),false 刪除桌面快捷方式 AutoStart.Instance.SetDesktopQuick(true);
完整代碼
引用以下命名空間:
//添加引用,在 Com 中搜索 Windows Script Host Object Model using IWshRuntimeLibrary; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO;
AutoStart類代碼:
public class AutoStart { #region 公開 /// <summary> /// 唯一實(shí)例,也可以自定義實(shí)例 /// </summary> public static AutoStart Instance { get; private set; } = new AutoStart(); /// <summary> /// 快捷方式描述,默認(rèn)值是當(dāng)前的進(jìn)程名 /// </summary> public string QuickDescribe { get; set; } = Process.GetCurrentProcess().ProcessName; /// <summary> /// 快捷方式名稱,默認(rèn)值是當(dāng)前的進(jìn)程名 /// </summary> public string QuickName { get; set; } = Process.GetCurrentProcess().ProcessName; /// <summary> /// 自啟動(dòng)窗口類型,默認(rèn)值是正常窗口 /// </summary> public WshWindowStyle WindowStyle { get; set; } = WshWindowStyle.WshNormalFocus; /// <summary> /// 設(shè)置開機(jī)自動(dòng)啟動(dòng)-只需要調(diào)用改方法就可以了參數(shù)里面的bool變量是控制開機(jī)啟動(dòng)的開關(guān)的,默認(rèn)為開啟自啟啟動(dòng) /// </summary> /// <param name="onOff">自啟開關(guān)</param> public void SetAutoStart(bool onOff = true) { if (onOff)//開機(jī)啟動(dòng) { //獲取啟動(dòng)路徑應(yīng)用程序快捷方式的路徑集合 List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath); //存在2個(gè)以快捷方式則保留一個(gè)快捷方式-避免重復(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, appAllPath, QuickDescribe,WindowStyle); } } else//開機(jī)不啟動(dòng) { //獲取啟動(dòng)路徑應(yīng)用程序快捷方式的路徑集合 List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath); //存在快捷方式則遍歷全部刪除 if (shortcutPaths.Count > 0) { for (int i = 0; i < shortcutPaths.Count; i++) { DeleteFile(shortcutPaths[i]); } } } //創(chuàng)建桌面快捷方式-如果需要可以取消注釋 //CreateDesktopQuick(desktopPath, QuickName, appAllPath); } /// <summary> /// 在桌面上創(chuàng)建快捷方式-如果需要可以調(diào)用 /// </summary> public void SetDesktopQuick(bool isCreate) { string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); List<string> shortcutPaths = GetQuickFromFolder(desktopPath, appAllPath); if (isCreate) { //沒有就創(chuàng)建 if (shortcutPaths.Count < 1) { CreateShortcut(desktopPath, QuickName, appAllPath, QuickDescribe, WshWindowStyle.WshNormalFocus); } } else { //有就刪除 for (int i = 0; i < shortcutPaths.Count; i++) { DeleteFile(shortcutPaths[i]); } } } ? #endregion 公開 ? #region 私有 ? /// <summary> /// 自動(dòng)獲取系統(tǒng)自動(dòng)啟動(dòng)目錄 /// </summary> private string systemStartPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); ? /// <summary> /// 自動(dòng)獲取程序完整路徑 /// </summary> private string appAllPath = Process.GetCurrentProcess().MainModule.FileName; /// <summary> /// 自動(dòng)獲取桌面目錄 /// </summary> private string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); /// <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, WshWindowStyle windowStyle, string iconLocation = null) { try { //目錄不存在則創(chuàng)建 if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); //合成路徑 string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName)); //存在則不創(chuàng)建 if (System.IO.File.Exists(shortcutPath)) return true; //添加引用 Com 中搜索 Windows Script Host Object Model WshShell shell = new IWshRuntimeLibrary.WshShell(); //創(chuàng)建快捷方式對(duì)象 IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); //指定目標(biāo)路徑 shortcut.TargetPath = targetPath; //設(shè)置起始位置 shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); //設(shè)置運(yùn)行方式,默認(rèn)為常規(guī)窗口 shortcut.WindowStyle = (int)windowStyle; //設(shè)置備注 shortcut.Description = description; //設(shè)置圖標(biāo)路徑 shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation; //保存快捷方式 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)開啟了自動(dòng)啟動(dò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ù)路徑刪除文件-用于取消自啟時(shí)從計(jì)算機(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); } } #endregion 私有 }
總結(jié)
在本文中,我們探討了如何使用C#語言實(shí)現(xiàn)應(yīng)用程序在系統(tǒng)啟動(dòng)時(shí)自動(dòng)運(yùn)行的功能,同時(shí)避免了對(duì)管理員權(quán)限的需求。通過這種方法,用戶可以在不進(jìn)行額外配置的情況下,確保應(yīng)用程序隨系統(tǒng)啟動(dòng)而自動(dòng)加載,極大地提高了使用的便捷性和程序的可用性。
最后
到此這篇關(guān)于C#實(shí)現(xiàn)軟件開機(jī)自啟動(dòng)功能(不需要管理員權(quán)限)的文章就介紹到這了,更多相關(guān)C#軟件開機(jī)自啟動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#爬蟲基礎(chǔ)之HttpClient獲取HTTP請(qǐng)求與響應(yīng)
這篇文章介紹了C#使用HttpClient獲取HTTP請(qǐng)求與響應(yīng)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05Unity Shader實(shí)現(xiàn)序列幀動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)序列幀動(dòng)畫效果 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02Unity TextMeshPro實(shí)現(xiàn)富文本超鏈接默認(rèn)字體追加字體
這篇文章主要為大家介紹了Unity TextMeshPro實(shí)現(xiàn)富文本超鏈接默認(rèn)字體追加字體示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01C#實(shí)現(xiàn)WinForm控件焦點(diǎn)的獲取與失去
在一個(gè)數(shù)據(jù)輸入表單中,當(dāng)用戶從一個(gè)文本框切換到另一個(gè)文本框時(shí),需要準(zhǔn)確地判斷焦點(diǎn)的轉(zhuǎn)移,以便進(jìn)行數(shù)據(jù)驗(yàn)證、提示信息顯示等操作,本文將探討 Winform 控件獲取與失去焦點(diǎn)的相關(guān)知識(shí),需要的朋友可以參考下2025-01-01C#實(shí)現(xiàn)Winform無邊框移動(dòng)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)Winform無邊框移動(dòng)的方法,涉及C#針對(duì)WinForm窗口操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09