Dot-Net

我可以為 ClickOnce 應用程序創建桌面圖示嗎?

  • September 30, 2008

我在一些 ClickOnce 文章中讀到 ClickOnce 不允許您為應用程序創建桌面圖示。有沒有辦法解決?

在 Visual Studio 2005 中,ClickOnce無法創建桌面圖示,但現在可以在 Visual Studio 2008 SP1 中使用。在 Visual Studio 2005 中,您可以使用以下程式碼在應用程序啟動時為您創建一個桌面圖示。

幾個月來,我已經在幾個項目中使用了這段程式碼,沒有任何問題。我必須說,我所有的應用程序都部署在受控環境中的 Intranet 上。此外,解除安裝應用程序時不會刪除該圖示。此程式碼創建 ClickOnce 創建的開始菜單上的快捷方式的快捷方式。

private void CreateDesktopIcon()
{
   ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;

       if (ad.IsFirstRun)
       {
           Assembly assembly = Assembly.GetEntryAssembly();
           string company = string.Empty;
           string description = string.Empty;

           if (Attribute.IsDefined(assembly, typeof(AssemblyCompanyAttribute)))
           {
               AssemblyCompanyAttribute ascompany =
                 (AssemblyCompanyAttribute)Attribute.GetCustomAttribute(
                   assembly, typeof(AssemblyCompanyAttribute));

               company = ascompany.Company;
           }
           if (Attribute.IsDefined(assembly, typeof(AssemblyDescriptionAttribute)))
           {
               AssemblyDescriptionAttribute asdescription =
                 (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(
                   assembly, typeof(AssemblyDescriptionAttribute));

               description = asdescription.Description;
           }
           if (!string.IsNullOrEmpty(company))
           {
               string desktopPath = string.Empty;
               desktopPath = string.Concat(
                               Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                               "\\",
                               description,
                               ".appref-ms");

               string shortcutName = string.Empty;
               shortcutName = string.Concat(
                                Environment.GetFolderPath(Environment.SpecialFolder.Programs),
                                "\\",
                                company,
                                "\\",
                                description,
                                ".appref-ms");

               System.IO.File.Copy(shortcutName, desktopPath, true);
           }
       }
   }
}

似乎有一種方法可以在 ClickOnce 的桌面上放置一個圖示。

  1. 升級到 Visual Studio 2008 SP 1,在項目屬性視窗的發布部分的選項頁面中,將有一個放置在桌面上的圖示複選框。
  2. 第二個選項是向應用程序添加程式碼,在應用程序第一次執行時將快捷方式複製到桌面。請參閱部落格文章*如何將桌面快捷方式添加到 ClickOnce 部署應用程序*。

引用自:https://stackoverflow.com/questions/152188