一、添加 “Interop.IWshRuntimeLibrary.dll” 至Plugins文件夹下
二、代码
using IWshRuntimeLibrary; using File = System.IO.File; private void Awake() {
string StartupPath =
Environment.GetFolderPath(Environment.SpecialFolder.Startup); //删除软件开机自启 if
(File.Exists($"{StartupPath}/halo.lnk")) {
File.Delete($"{StartupPath}/halo.lnk"); } //设置软件开机自启
//CreateShortcut(StartupPath, "halo", @"G:\桌面\git-lfs-windows-v3.0.2.exe"); }
/// <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 = null, string iconLocation = null) { try { if
(!Directory.Exists(directory)) Directory.CreateDirectory(directory); //
添加引用com中搜索Windows Script Host Object Model, 如果在unity中使用则需下载
Interop.IWshRuntimeLibrary.dll 并放到代码同一文件夹 string shortcutPath =
Path.Combine(directory, $"{shortcutName}.lnk"); WshShell shell = new
WshShell(); IWshShortcut shortcut =
(IWshShortcut)shell.CreateShortcut(shortcutPath);// 创建快捷方式对象
shortcut.TargetPath = targetPath;// 指定目标路径 shortcut.WorkingDirectory =
Path.GetDirectoryName(targetPath);//设置起始位置 shortcut.WindowStyle = 1;//
设置运行方式,默认为常规窗口 shortcut.Description = description;// 设置备注 shortcut.IconLocation
= string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;//设置图标路径
shortcut.Save();// 保存快捷方式 return true; } catch (Exception ex) {
Debug.LogException(ex); } return false; }
三、软件开机自启存放位置:
C:\Users\Xmj\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup