Here are some console application routines for a Windows desktop shortcut.
Needs a reference to 'Windows Script Host Object Model', under References - COM.
Example usage:
CreateDesktopShortcut("MyWebApp", "https://localhost:443", true);
private static string CreateDesktopShortcut(string appname, string appPathFull, bool create)
{
try
{
var desktopPathName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory), appname + ".lnk");
CreateShortcut(desktopPathName, appPathFull, create);
return desktopPathName;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return string.Empty;
}
private static void CreateShortcut(string shortcutPathName, string shortcutTarget, bool create, string arguments = "")
{
if (create)
{
WshShell myShell = new WshShell();
IWshShortcut myShortcut = myShell.CreateShortcut(shortcutPathName) as IWshShortcut;
myShortcut.TargetPath = shortcutTarget;
if (shortcutTarget.StartsWith("http"))
{
if (File.Exists("MyIcon.ico"))
{
myShortcut.IconLocation = "MyIcon.ico";
}
}
else
{
myShortcut.IconLocation = shortcutTarget + ",0";
myShortcut.WorkingDirectory = Directory.GetParent(shortcutTarget).ToString();
}
myShortcut.Arguments = arguments;
myShortcut.Save();
}
else
{
if (File.Exists(shortcutPathName))
{
File.SetAttributes(shortcutPathName, FileAttributes.Normal);
File.Delete(shortcutPathName);
}
}
}