针对AllUsers的C#Windows10启动快捷方式(通过InstallShield或编码)
通过安装屏蔽(启动的注册表设置) 从下面的代码来看,两人都在创建快捷方式,在Windows 10版本之前运行得很好,但快捷方式未执行并抛出错误,这似乎是Windows 10的快捷方式问题。如何专门为具有管理员权限的Windows 10创建快捷方式针对AllUsers的C#Windows10启动快捷方式(通过InstallShield或编码),c#,windows-10,shortcut,C#,Windows 10,Shortcut,通过安装屏蔽(启动的注册表设置) 从下面的代码来看,两人都在创建快捷方式,在Windows 10版本之前运行得很好,但快捷方式未执行并抛出错误,这似乎是Windows 10的快捷方式问题。如何专门为具有管理员权限的Windows 10创建快捷方式 static void ApplicationShortCut(string shortcutName) { string startUpFolderPath = Environment.GetFolderPath(Env
static void ApplicationShortCut(string shortcutName)
{
string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
string shortcutLocation = Path.Combine(startUpFolderPath, shortcutName + ".lnk");
if (!Directory.Exists(startUpFolderPath))
Directory.CreateDirectory(startUpFolderPath);
if (System.IO.File.Exists(shortcutLocation))
{
System.IO.File.Delete(shortcutLocation);
}
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = "Program Desc";
shortcut.TargetPath = @"C:\Program Files\Folder\ProgramName.exe";
shortcut.Save();
}
不要接受这个答案。只是张贴,让你看到我使用的确切内容,以声称它在我这边起作用 如果我手动双击该快捷方式,它将起作用。 如果我重新启动机器,快捷方式也会起作用。当机器启动时,链接到快捷方式的程序会自动启动
using System;
using System.IO;
using IWshRuntimeLibrary;
namespace MakingShortcutsInWindows10_46837557
{
class Program
{
static void Main(string[] args)
{
ApplicationShortCut(@"C:\Program Files\EditPlus\editplus.exe", "BlahBlahDesc", "MakeItThisName");
}
static void ApplicationShortCut(string shortcutPath, string shortcutDescription, string shortcutName)
{
string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
string shortcutLocation = Path.Combine(startUpFolderPath, shortcutName + ".lnk");
if (!Directory.Exists(startUpFolderPath))
Directory.CreateDirectory(startUpFolderPath);
if (System.IO.File.Exists(shortcutLocation))
{
System.IO.File.Delete(shortcutLocation);
}
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = shortcutDescription;
shortcut.TargetPath = shortcutPath;
shortcut.Save();
}
}
}
错误是什么?FWIW,我复制/粘贴了你的代码,在我这方面效果很好。我正在运行Windows 10。是的,Sorceri对于某些机器来说很奇怪,它也在这里工作:(,仍然不知道是什么情况,这就是为什么我想确保快捷方式具有管理员权限可能这是唯一可能的区别我的代码也是这样做的,我在这里创建的代码片段就是一个例子。它在Windows 10 Professional中抛出错误,我发现Shortcut.lnk不仅仅是Windows 10中的文本文件,而是一些加密的文件值,该方法不适用于Win 10。但它需要添加一个参考:“Project>Add reference>COM>Windows脚本主机对象模型”。此外,使用
应用程序。ExecutablePath
是获取当前打开的可执行文件路径的好方法,因此您可以为其创建快捷方式。