【问题标题】:How to create a relative Windows shortcut in C#?如何在 C# 中创建相对的 Windows 快捷方式?
【发布时间】:2018-01-22 11:47:27
【问题描述】:

我在这里尝试结合两个问题:1)How to create a relative shortcut in Windows 和 2)How to createa Windows shortcut in C#

我有以下代码来创建快捷方式(基于我看到的问题),但是在分配 shortcut.TargetPath 时会引发异常:“值不在预期范围内”

public void CreateShortcut() {
    IWshRuntimeLibrary.WshShell wsh = new IWshRuntimeLibrary.WshShell();
    IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(Path.Combine(outputFolder, "sc.lnk")) as IWshRuntimeLibrary.IWshShortcut;
    shortcut.Arguments = "";
    shortcut.TargetPath = "%windir%\\system32\\cmd.exe /c start \"\" \"%CD%\\folder\\executable.exe\"";
    shortcut.WindowStyle = 1;
    shortcut.Description = "executable";
    shortcut.WorkingDirectory = "%CD%";
    shortcut.IconLocation = "";
    shortcut.Save();
}

如何解决这个问题并创建我的相对快捷方式?

注意:我不想编写批处理脚本来执行此操作,因为我的软件很可能安装在用户无法访问命令行的 PC 上 - 我们的客户通常非常锁定机器并且几乎可以肯定没有运行批处理文件的正确权限。如果您对如何创建“便携式”文件夹(我的 .exe 在子文件夹中)有任何建议,用户只需双击顶级文件夹中的某些内容即可运行 exe,我愿意建议!

【问题讨论】:

  • 不能那个“东西”是可执行文件吗?
  • 可以,但这是不得已而为之。我希望使整个“包装”尽可能轻巧且无体积。

标签: c# windows


【解决方案1】:

您不能将参数放在TargetPath-属性中。 (见MSDN

将它们放入Arguments- 属性中:

public void CreateShortcut() {
    IWshRuntimeLibrary.WshShell wsh = new IWshRuntimeLibrary.WshShell();
    IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(Path.Combine(outputFolder, "sc.lnk")) as IWshRuntimeLibrary.IWshShortcut;
    shortcut.Arguments = "/c start \"\" \"%CD%\\folder\\executable.exe\"";
    shortcut.TargetPath = "%windir%\\system32\\cmd.exe";
    shortcut.WindowStyle = 1;
    shortcut.Description = "executable";
    shortcut.WorkingDirectory = "%CD%";
    shortcut.IconLocation = "P:\ath\to\any\icon.ico";
    shortcut.Save();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-11
    • 2016-02-23
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多