欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > C# 实现软件开机自启动(不需要管理员权限)

C# 实现软件开机自启动(不需要管理员权限)

2025/6/22 13:54:55 来源:https://blog.csdn.net/u011966339/article/details/148390843  浏览:    关键词:C# 实现软件开机自启动(不需要管理员权限)

本文参考C#/WPF/WinForm/程序实现软件开机自动启动的两种常用方法,将里面中的第一种方法做了封装成AutoStart类,使用时直接两三行代码就可以搞定。

自启动的原理是将软件的快捷方式创建到计算机的自动启动目录下(不需要管理员权限),这种方法更加通用、限制更少。

使用方法

使用方法如下:

//快捷方式的描述、名称的默认值是当前的进程名,自启动默认为正常窗口,一般情况下不需要手动设置
//设置快捷方式的描述,
AutoStart.Instance.QuickDescribe = "软件描述";
//设置快捷方式的名称
AutoStart.Instance.QuickName = "软件名称";
//设置自启动的窗口类型,后台服务类的软件可以设置为最小窗口
AutoStart.Instance.WindowStyle = WshWindowStyle.WshMinimizedFocus;//快捷方式设置true时,有就忽略、没有就创建,自启动快捷方式只能存在一个
//设置开机自启动,true 自启动,false 不自启动
AutoStart.Instance.SetAutoStart(SysParam.Instance.OnOff);
//设置桌面快捷方式,true 创建桌面快捷方式(有就跳过,没有就创建),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 classAutoStart
{#region 公开/// <summary>/// 唯一实例,也可以自定义实例/// </summary>publicstatic AutoStart Instance { get; privateset; } = new AutoStart();/// <summary>/// 快捷方式描述,默认值是当前的进程名/// </summary>publicstring QuickDescribe { get; set; } = Process.GetCurrentProcess().ProcessName;/// <summary>/// 快捷方式名称,默认值是当前的进程名/// </summary>publicstring QuickName { get; set; } = Process.GetCurrentProcess().ProcessName;/// <summary>/// 自启动窗口类型,默认值是正常窗口/// </summary>public WshWindowStyle WindowStyle { get; set; } = WshWindowStyle.WshNormalFocus;/// <summary>/// 设置开机自动启动-只需要调用改方法就可以了参数里面的bool变量是控制开机启动的开关的,默认为开启自启启动/// </summary>/// <param name="onOff">自启开关</param>public void SetAutoStart(bool onOff = true){if (onOff)//开机启动{//获取启动路径应用程序快捷方式的路径集合List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);//存在2个以快捷方式则保留一个快捷方式-避免重复多于if (shortcutPaths.Count >= 2){for (int i = 1; i < shortcutPaths.Count; i++){DeleteFile(shortcutPaths[i]);}}elseif (shortcutPaths.Count < 1)//不存在则创建快捷方式{CreateShortcut(systemStartPath, QuickName, appAllPath, QuickDescribe,WindowStyle);}}else//开机不启动{//获取启动路径应用程序快捷方式的路径集合List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);//存在快捷方式则遍历全部删除if (shortcutPaths.Count > 0){for (int i = 0; i < shortcutPaths.Count; i++){DeleteFile(shortcutPaths[i]);}}}//创建桌面快捷方式-如果需要可以取消注释//CreateDesktopQuick(desktopPath, QuickName, appAllPath);}/// <summary>/// 在桌面上创建快捷方式-如果需要可以调用/// </summary>public void SetDesktopQuick(bool isCreate){string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);List<string> shortcutPaths = GetQuickFromFolder(desktopPath, appAllPath);if (isCreate){//没有就创建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>/// 自动获取系统自动启动目录/// </summary>privatestring systemStartPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);/// <summary>/// 自动获取程序完整路径/// </summary>privatestring appAllPath = Process.GetCurrentProcess().MainModule.FileName;/// <summary>/// 自动获取桌面目录/// </summary>privatestring desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);/// <summary>///  向目标路径创建指定文件的快捷方式/// </summary>/// <param name="directory">目标目录</param>/// <param name="shortcutName">快捷方式名字</param>/// <param name="targetPath">文件完全路径</param>/// <param name="description">描述</param>/// <param name="iconLocation">图标地址</param>/// <returns>成功或失败</returns>private bool CreateShortcut(string directory, string shortcutName, string targetPath, string description, WshWindowStyle windowStyle, string iconLocation = null){try{//目录不存在则创建if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);//合成路径string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName));//存在则不创建if (System.IO.File.Exists(shortcutPath)) returntrue;//添加引用 Com 中搜索 Windows Script Host Object ModelWshShell shell = new IWshRuntimeLibrary.WshShell();//创建快捷方式对象IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);//指定目标路径shortcut.TargetPath = targetPath;//设置起始位置shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);//设置运行方式,默认为常规窗口shortcut.WindowStyle = (int)windowStyle;//设置备注shortcut.Description = description;//设置图标路径shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;//保存快捷方式shortcut.Save();returntrue;}catch (Exception ex){string temp = ex.Message;temp = "";}returnfalse;}/// <summary>/// 获取指定文件夹下指定应用程序的快捷方式路径集合/// </summary>/// <param name="directory">文件夹</param>/// <param name="targetPath">目标应用程序路径</param>/// <returns>目标应用程序的快捷方式</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>/// 获取快捷方式的目标文件路径-用于判断是否已经开启了自动启动/// </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 = 当前快捷方式文件IWshShortcut类.TargetPath;//快捷方式文件指向的目标目录.Text = 当前快捷方式文件IWshShortcut类.WorkingDirectory;return shortct.TargetPath;}else{return"";}}/// <summary>/// 根据路径删除文件-用于取消自启时从计算机自启目录删除程序的快捷方式/// </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 私有
}

总结

在本文中,我们探讨了如何使用C#语言实现应用程序在系统启动时自动运行的功能,同时避免了对管理员权限的需求。通过这种方法,用户可以在不进行额外配置的情况下,确保应用程序随系统启动而自动加载,极大地提高了使用的便捷性和程序的可用性。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词