8

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();
4

3 に答える 3

4

お世話になった皆様、ありがとうございました... わかりました。答えとして投稿したくありませんでしたが、他の誰かがこの同じ問題に遭遇した場合に備えて考えました..

コードに問題はなかったことがわかりました。Panhandel は、「ターゲット パスが存在しない場合にのみ、最初の結果を達成した」という発言をしたときに、解決策を見つける場所の手がかりを与えてくれました。彼は常に正しい結果を得ており、ディレクトリが存在しないときに得た結果しか得ていなかったので...問題は、プログラムでディレクトリを1行で作成し、次にアイコンを作成することである可能性があることに気付きました.. . ディレクトリが完全に作成されるまで、システムにもっと時間を与える必要がありました

于 2013-06-19T16:24:18.057 に答える