2012-11-06 34 views
0

我正嘗試創建一個快捷方式,以便在用戶向設備添加配置文件時我的程序創建的文件夾。該文件夾的快捷方式鏈接應該顯示在用戶收藏夾(Windows 7資源管理器中的圖書館)中,並從我們的項目中獲得logo.ico。我以爲我必須使用IWshRuntimeLibrary,但代碼隨機停在我身上,不會返回任何錯誤...有什麼幫助?在運行時創建Windows 7快捷方式

注意: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; 
} 
+0

在什麼時候它會失敗? –

+0

「if(!File.Exists ...)」和shortcut.Save()失敗。 –

+0

剛剛發現錯誤...配置文件和Windows用戶具有相同的名稱...所以.lnk無法創建。 –

回答

相關問題