ユーザーがプロファイルをデバイスに追加したときにプログラムが作成するフォルダーへのショートカットを作成しようとしています。フォルダーへのショートカット リンクは、ユーザーのお気に入り (Windows 7 エクスプローラーの横にあるライブラリ) に表示され、プロジェクトの 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;
}