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();
Cœur
  • 37,241
  • 25
  • 195
  • 267
Anthony Griggs
  • 1,469
  • 2
  • 17
  • 39
  • 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 05 '13 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. – Anthony Griggs Jun 06 '13 at 00: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 06 '13 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... – Anthony Griggs Jun 06 '13 at 15:06
  • 2
    What is the question that is being posed by the OP? – STLDev Jun 09 '13 at 02:30
  • Is there any correlation between the installation mode and the result you're getting from this code? – C.B. Jun 11 '13 at 07:57
  • @STLDeveloper : The implied question is how to modify this code to reliably produce result #3. – C.B. Jun 11 '13 at 07:58
  • @C.B.- Really? Please take another look. When he says, "Or lastly which is of course my favorite...", he's not saying that's the result he favors. He's saying that it's the strangest result he's seen. At no place does the OP explain what the problem is *with the code he's provided*. I contend that there's no real question here. – STLDev Jun 11 '13 at 09:27
  • Thank you everyone for your help... I figured it out. 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. – Anthony Griggs Jun 19 '13 at 16:15

3 Answers3

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

Anthony Griggs
  • 1,469
  • 2
  • 17
  • 39
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/");
}
Kauê Gimenes
  • 1,278
  • 1
  • 13
  • 30
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.

David E
  • 1,384
  • 9
  • 14