Hanny Hanny - 1 year ago 96
C# Question

add specific URL to shortcut target C#

Trying to create a few different shortcuts to various URLS on a Desktop with the following method:

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

//shortcut.Description = "My shortcut description"; // The description of the shortcut
//shortcut.IconLocation = @"c:\myicon.ico"; // The icon of the shortcut
shortcut.TargetPath =" \" "+targetFileLocation+ " \" " + " https://www.somewebsite.com" ; // The path of the file that will launch when the shortcut is run
shortcut.Save(); // Save the shortcut

It errors out if I try adding anything to the targetFileLocation.

I use it like this:

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

If I change this line in the method to this it completes without error:

shortcut.TargetPath = targetFileLocation ;

The shortcut is put on the desktop - but without the additional https://www.somewebsite.com added to the target - so it just opens the browser without directing it to the website.

I'm trying to create a few shortcuts that open explorer, but make it navigate to particular websites.

Answer Source

Two things are wrong:

  1. You don't need "" around the path to iexplore.exe
  2. You can't add the website address to the path, it must be an argument

Change the following code:

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

To this:

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

The rest of the method is fine as it is.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download