2010-03-16 8 views
3

J'écris le programme d'installation pour mon programme en utilisant Windows Installer (pas wix). Dans certains cas, je souhaite annuler le processus d'installation de mon action personnalisée. A côté, je veux montrer un message d'erreur avec mon texte. Comment ça?Annuler l'installation à partir de mon action personnalisée

C#, .Net 3.5

Répondre

3

Vous pouvez le faire en créant une action personnalisée d'erreur. Définissez la condition de l'action personnalisée error comme étant la condition d'échec (si la condition est évaluée à true, l'installation échouera) et définissez le message sur votre texte personnalisé.

1

chose de travail pour moi:

Contexte: pour une raison quelconque, je dois vérifier si l'utilisateur a 3.5SP1 installé, annuler l'installation et redirection vers la page de téléchargement correcte sinon.

Etape 1: Modifier votre installateur comme celui-ci

[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); 
    } 

Etape 2: Utilisez ce code pour héberger votre MessageBox (je l'ai pris ailleurs, pas le temps de le trouver par moi-même)

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; 
    } 
} 

Étape 3: Tuer le 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); 
} 

Mélangez-le avec une cuillère pas avec un shaker et servez;)

+2

-1. Tuer msiexec est maléfique - il est similaire à couper un arbre pour récolter les fruits ... –

+0

qui risque de laisser des fichiers derrière lorsque vous tuez le processus msiexec en appelant Terminate. Il est préférable de lancer une nouvelle InstallException ("Utilisateur annulé"); (et de ne pas l'attraper) après la base.Install (savedState); Le nettoyage de restauration ultérieur fonctionnera mieux de cette façon. – dmihailescu