【发布时间】:2012-10-26 13:04:52
【问题描述】:
我正在尝试为我的程序在用户将配置文件添加到设备时创建的文件夹创建快捷方式。该文件夹的快捷方式链接应该出现在用户的收藏夹(Windows 7 Explorer 中的库)中,并具有我们项目中的 logo.ico。我以为我必须使用 IWshRuntimeLibrary,但代码会随机停在我身上并且不会返回任何错误...有什么帮助吗?
注意:profilePath、profileName 和 executablePath 都返回正确的数据并指向我尝试访问和修改的项目的正确位置。
public static bool CreateProfileShortcut(string profilePath, string profileName, string executablePath)
{
bool success = false;
string favoritesPath = "";
IWshRuntimeLibrary.IWshShell wsh;
IWshRuntimeLibrary.IWshShortcut shortcut;
try
{
if (!String.IsNullOrWhiteSpace(profilePath) && Directory.Exists(profile.Path))
{
favoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
if (!File.Exists(favoritesPath + "\\Links\\" + profileName + ".lnk"))
{
wsh = new IWshRuntimeLibrary.WshShell();
shortcut = (IWshRuntimeLibrary.IWshShortcut)wsh.CreateShortcut(favoritesPath + "\\Links\\" + profileName + ".lnk");
shortcut.TargetPath = profilePath;
shortcut.IconLocation = executablePath + "\\Icons\\logo.ico";
shortcut.Save();
}
}
}
catch (Exception e)
{
MessageBox.Show(MessageBoxError(new string[] { e.TargetSite.Name, e.Message, "MinorError" }));
success = false;
}
return success;
}
【问题讨论】:
-
它到底在什么时候失败了?
-
它在“if (!File.Exists...)”和shortcut.Save() 周围失败。
-
刚刚发现错误...配置文件和 windows 用户具有相同的名称...所以无法创建 .lnk。
标签: c# windows-7 .net-4.0 shortcuts