C#Windows10启动(通过InstallShield或编码)为所有用户的快捷方式

C#Windows10启动(通过InstallShield或编码)为所有用户的快捷方式

问题描述:

通过安装盾(启动时的注册表设置) ,并从下面的代码都创建快捷方式,在Windows 10版本之前运行得很好,但快捷方式不执行和抛出错误,似乎是Windows 10的快捷方式问题。如何使用管理员权限专门为Windows 10创建快捷方式C#Windows10启动(通过InstallShield或编码)为所有用户的快捷方式

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(); 

    } 
+1

什么是错误? – Sorceri

+0

FWIW,我复制/粘贴你的代码,它在我身边很好地工作。我正在运行的Windows 10. –

+0

是的Sorceri它的一些机器的奇怪它的工作在这里:(仍然不知道什么情况下,这就是为什么我想确保快捷方式与管理权限可能是唯一的区别 – Kay

不要接受这个答案。只是发布,让你看到我用什么来声称它在我的结尾工作。

如果手动双击它,快捷方式将起作用。 如果我重新启动机器,快捷方式也可以使用。当机器启动时,链接到快捷方式的程序自行启动。

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(); 
     } 

    } 
} 
+0

我的代码是同样,我在这里创建的代码片段就是例子,它在Windows 10 Professional中引发错误,我发现shortcut.lnk不仅仅是Windows 10中的文本文件,而是具有一些加密值,这种方法不会发生 – Kay

+1

在Win 10下工作。但是它需要添加一个引用:“Project> Add Reference> COM> Windows Script Host Object Model”。另外使用Application.ExecutablePath是获取当前打开的可执行文件路径的好方法,所以你可以创建它的一个快捷方式。 –