以编程方式创建目录快捷方式的C#并不总是有效

Ant*_*ggs 8 c# directory shortcut

我试图以编程方式创建目录的快捷方式.我找到了很多例子,但似乎没有一个例子可行.

我在生成的快捷方式属性中观察到三个不同的结果:

  1. 文件的快捷方式类型被指定为"快捷方式(.lnk)",这会导致弹出打开对话框,要求我附加一个扩展名.

  2. "文件"的"快捷方式"属性被指定为"文件",双击时绝对没有任何内容.

  3. 或者最后,这当然是我最喜欢的......文件的快捷方式属性被指定为:"文件夹",它的工作方式应该如此.

这是我目前正在使用的代码......我尝试了一些这方面的变化.

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();
Run Code Online (Sandbox Code Playgroud)

Ant*_*ggs 4

谢谢大家的帮助...我明白了。我不想将其作为答案发布,但想以防万一其他人碰巧遇到同样的问题......尽管我对自己的疏忽感到羞愧。

事实证明代码没有任何问题。Panhandel 在发表以下声明时给了我一个线索:“当目标路径不存在时,我才获得了第一个结果。” 由于他总是得到正确的结果,而他只得到我在目录不存在时得到的结果...我意识到问题可能是我在一行中以编程方式创建目录,然后在下一行中创建图标.. .我需要给系统更多的时间来完全创建目录