2010-03-16 32 views
3

我正在使用Windows Installer(而不是wix)爲我的程序編寫安裝程序。 在某些情況下,我想從我的自定義操作中取消安裝過程。除此之外,我想用我的文字顯示錯誤信息。 這是怎麼回事?從我的自定義操作取消安裝

C#,.net 3.5

回答

3

您可以通過創建錯誤自定義操作來完成此操作。將錯誤自定義操作的條件設置爲失敗條件(如果條件評估爲true,安裝將失敗)並將消息設置爲您的自定義文本。我

1

工作的事情:

背景:由於某種原因,我需要檢查,如果用戶安裝了3.5SP1,取消安裝並重定向到正確的下載頁面如果不是。

第一步: 修改您的安裝這樣

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)] 
    public override void Install(IDictionary stateSaver) 
    { 
     //Check if the FrameWork 3.5SP1 is installed 
     if (mycondition) 
     { 
      //3.5SP1 is installed, ask for framework install 
      if (TopMostMessageBox.Show("body", "title", MessageBoxButtons.YesNo) == DialogResult.Yes) 
       System.Diagnostics.Process.Start("http://Microsoft FRW Link"); 

      WindowHandler.Terminate(); 
     } 
     else 
      base.Install(stateSaver); 
    } 

第二步: 使用該代碼託管您的MessageBox(我把它在其他地方,沒有時間由我自己找到它)

static public class TopMostMessageBox 
{ 
    static public DialogResult Show(string message) 
    { 
     return Show(message, string.Empty, MessageBoxButtons.OK); 
    } 

    static public DialogResult Show(string message, string title) 
    { 
     return Show(message, title, MessageBoxButtons.OK); 
    } 

    static public DialogResult Show(string message, string title, 
     MessageBoxButtons buttons) 
    { 
     // Create a host form that is a TopMost window which will be the 

     // parent of the MessageBox. 

     Form topmostForm = new Form(); 
     // We do not want anyone to see this window so position it off the 

     // visible screen and make it as small as possible 

     topmostForm.Size = new System.Drawing.Size(1, 1); 
     topmostForm.StartPosition = FormStartPosition.Manual; 
     System.Drawing.Rectangle rect = SystemInformation.VirtualScreen; 
     topmostForm.Location = new System.Drawing.Point(rect.Bottom + 10, 
      rect.Right + 10); 
     topmostForm.Show(); 
     // Make this form the active form and make it TopMost 

     topmostForm.Focus(); 
     topmostForm.BringToFront(); 
     topmostForm.TopMost = true; 
     // Finally show the MessageBox with the form just created as its owner 

     DialogResult result = MessageBox.Show(topmostForm, message, title, 
      buttons); 
     topmostForm.Dispose(); // clean it up all the way 


     return result; 
    } 
} 

第三步: 殺死MSIEXEC

internal static class WindowHandler 
{ 
    internal static void Terminate() 
    { 
     var processes = Process.GetProcessesByName("msiexec").OrderBy(x => x.StartTime); \\DO NOT FORGET THE ORDERBY!!! It makes the msi processes killed in the right order 
     foreach (var process in processes) 
     { 
      var hWnd = process.MainWindowHandle.ToInt32(); 
      ShowWindow(hWnd, 0); //This is to hide the msi window and only show the popup 
      try 
      { 
       process.Kill(); 
      } 
      catch 
      { 
      } 
     } 
    } 

    [DllImport("User32")] 
    private static extern int ShowWindow(int hwnd, int nCmdShow); 
} 

用不帶振動篩的勺子將其混合並送達;)

+2

-1。殺死msiexec是邪惡的 - 它類似於砍樹收割水果...... – 2011-10-12 05:59:43

+0

當你通過調用Terminate殺死msiexec進程時可能會留下一些文件。最好是在base.Install(savedState);之後拋出新的InstallException(「User cancelled」);(並且不要捕獲它)。隨後的回滾清理將以這種方式更好地工作。 – dmihailescu 2013-09-19 16:03:19

相關問題