原理简介
本文参考c#实现软件开机自动启动的两种常用方法,将里面中的第一种方法做了封装成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 class autostart
{
#region 公开
/// <summary>
/// 唯一实例,也可以自定义实例
/// </summary>
public static autostart instance { get; private set; } = new autostart();
/// <summary>
/// 快捷方式描述,默认值是当前的进程名
/// </summary>
public string quickdescribe { get; set; } = process.getcurrentprocess().processname;
/// <summary>
/// 快捷方式名称,默认值是当前的进程名
/// </summary>
public string 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]);
}
}
else if (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>
private string systemstartpath = environment.getfolderpath(environment.specialfolder.startup);
/// <summary>
/// 自动获取程序完整路径
/// </summary>
private string appallpath = process.getcurrentprocess().mainmodule.filename;
/// <summary>
/// 自动获取桌面目录
/// </summary>
private string 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)) return true;
//添加引用 com 中搜索 windows script host object model
wshshell 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();
return true;
}
catch (exception ex)
{
string temp = ex.message;
temp = "";
}
return false;
}
/// <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#语言实现应用程序在系统启动时自动运行的功能,同时避免了对管理员权限的需求。通过这种方法,用户可以在不进行额外配置的情况下,确保应用程序随系统启动而自动加载,极大地提高了使用的便捷性和程序的可用性。
最后
到此这篇关于c#实现软件开机自启动功能(不需要管理员权限)的文章就介绍到这了,更多相关c#软件开机自启动内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论