9

I am trying to programatically create a shortcut to a directory. I have found numerous examples, but none appear to work realiably.

I observe three different results in the produced shortcut's properties:

  1. The Shortcut Type of File is assigned as "Shortcut(.lnk)" which cause the Open With dialog box to pop up asking me to attach an extension to it.

  2. The Shortcut Type of File property is assigned as "File" which does absolutely nothing when double clicked.

  3. Or lastly which is of course my favorite... the Shortcut Type of File property is assigned as: "File Folder" which works like it should.

Here is the code I am currently using... I've tried a few variations of this.

bool IsExists = false;
string icon = appPath + "Welcome.ico";

// Their is a difference to local and ClickOnce App locations... this compensates for it
IsExists = System.IO.File.Exists(icon);
if (!IsExists)
{
    icon = appPath + @"bin\Debug\Welcome.ico";

}

var desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
var target = (Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Artronix\Welcome To FreightWare Online\").Replace(@"\","/");
IWshShortcut shortcut;
var wshShell = new WshShellClass();
shortcut = (IWshShortcut)wshShell.CreateShortcut(Path.Combine(desktop, @"FreightWare Resources.lnk"));
shortcut.IconLocation = icon;
shortcut.TargetPath = Path.GetFullPath(target);
shortcut.Save();
9
  • 1
    I suspect appPath being the culprit with this much info. Using the second half of your code (and using my own set .ico file), I always get the third result. I only achieved the first result when the target path didn't exist. If you can narrow down under what conditions the results differ, that may help
    – panhandel
    Jun 5, 2013 at 22:34
  • Here is appPath: private string appPath = System.AppDomain.CurrentDomain.BaseDirectory; The difference lies in the fact that if I install this app locally via InstallShield or VS it installs the icon file within the same directory as appPath... but when I run a ClickOnce Web only installation for some reason it adds another bin and Debug folder within the appPath directory... thus the if statement if not found in appPath then look inside extended directory. Also note that the icon always displays within the shortcut so I know it is finding the correct path. Jun 6, 2013 at 0:16
  • I am very tempted to downvote, just because you are asking for upvotes. I'm pretty sure there's a rule against that somewhere, apart from it being an obvious internet faux-pas.
    – Mels
    Jun 6, 2013 at 12:27
  • I apologize... I do not know if it helps, but I really hesitated to ask. The only reason I requested it is that I have come to use this Forum a lot as I can most of the time get good quick answers to my questions... I've obtained some real good code from other developers and I've always felt bad that I could not upvote them... Almost every question I have had has already been answered so I've never really been able to get Cred event though like I said I'm on this forum at least 5 days a week... Jun 6, 2013 at 15:06
  • 2
    What is the question that is being posed by the OP?
    – STLDev
    Jun 9, 2013 at 2:30

3 Answers 3

4

Thank you everyone for your help... I figured it out. I didn't want to post it as an answer, but thought just in case someone else happened to come across this same problem... Although I feel sheepish about my oversight.

It turns out there was nothing wrong with the code. Panhandel gave me a clue to where to find the solution when he made the statement: " I only achieved the first result when the target path didn't exist." Since he was always getting the correct result and he only got the results I was getting when the directory did not exist... I realized the problem may be that I create the directory programatically in one line then in the next create the icon... I needed to give the system more time for the directory to be fully created

2

Try ShellLink:

using (ShellLink shortcut = new ShellLink())
{
    shortcut.Target = Application.ExecutablePath;
    shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
    shortcut.Description = "My Shorcut Name Here";
    shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
    shortcut.Save("%HOMEPATH%/Desktop/");
}
1

I had a slight variant on this issue...

I eventually discovered an additional condition:

  1. The target path has to point at a folder when the shortcut is created (as discussed above)
  2. But also - It appears the target path that's provided also has to be normalized - eg via Path.GetFullPath(possiblyUnNormalizedPath) see How can one get an absolute or normalized file path in .NET?

Hope this might help someone in the distant future avoid wasting an hour of their life.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.