2011年5月11日

Add an uninstall shortcut to setup project

使用 VS2010 建立 WPF 的 Setup Project,若希望在程式集的錄下,有一個解除安裝的按鈕,可以採用下列的方法。

  1. 在 Setup Project 的 User's Programs Menu 點選右鍵,選擇 Add / Project Output / Primary Output,並將名稱改為 Unstall xxx
  2. 將此 Shortcut 的 Argument 屬性設為 /u=[ProductCode]
  3. 上列設定將產生 unstall 項目,並在點選後,呼叫原 AP 程式,並帶入 /u=[ProductCode] 參數。參數中的 ProductCode 會自動轉換成 AP 的 guid。接著將下列的程式碼加到 WPF 的 App.xaml.cs 的 OnStartup(),來實際執行 unstall 的工作。

 
protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    // 解除安裝
         string[] arguments = Environment.GetCommandLineArgs();
    foreach (string argument in arguments)
    {
        string[] items = argument.Split(new char[] { '=' });
        if (items.Length >= 2 && items[0].ToLower() == "/u")
        {
            string guid = items[1];
            string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
            ProcessStartInfo info = new ProcessStartInfo(path + @"\msiexec", "/x" + guid); 
            Process.Start(info);
            this.Shutdown();
        }
    }
}
[Reference]
Add an uninstall start menu item to your .NET deployment project

沒有留言:

Deploying Vue & .NET with Google OAuth on GCP Cloud Run

Deploying Vue & .NET with Google OAuth on GCP Cloud Run Deploying Vue & .NET with Google OAuth on GCP Cloud Run...