将特定URL添加到快捷方式目标C#

时间:2016-11-10 14:09:49

标签: c# visual-studio

尝试使用以下方法为桌面上的各种URL创建一些不同的快捷方式:

public static void CreateShortcutWithURL(
    string shortcutName, string shortcutPath, string targetFileLocation)
{
    var shortcutLocation = Path.Combine(shortcutPath, shortcutName + ".lnk");
    var shell = new WshShell();
    var shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);

    // The description of the shortcut
    //shortcut.Description = "My shortcut description";

    // The icon of the shortcut
    //shortcut.IconLocation = @"c:\myicon.ico";

    // The path of the file that will launch when the shortcut is run
    shortcut.TargetPath = $" \" {targetFileLocation} \" https://www.somewebsite.com";

    shortcut.Save();
}

如果我尝试向targetFileLocation添加任何内容,则会出错。

我这样用:

CreateShortcutWithURL(
    "My Shortcut",
    Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
    @"C:\Program Files (x86)\Internet Explorer\iexplore.exe");

如果我在方法中将此行更改为此,则完成且没有错误:

shortcut.TargetPath = targetFileLocation ; 

快捷方式放在桌面上 - 但没有将额外的https://www.somewebsite.com添加到目标中 - 因此它只是打开浏览器而不会将其指向网站。

我正在尝试创建一些打开资源管理器的快捷方式,但要将其导航到特定网站。

1 个答案:

答案 0 :(得分:3)

有两件事是错的:

  1. 在iexplore.exe
  2. 的路径上,您不需要""
  3. 您无法将网站地址添加到路径中,它必须是参数
  4. 更改以下代码:

    shortcut.TargetPath =" \" "+targetFileLocation+ " \" " + " https://www.somewebsite.com" ; 
    

    对此:

    shortcut.TargetPath = targetFileLocation;
    shortcut.Arguments = @"https://www.google.com";
    

    方法的其余部分很好。