2016-04-22 7 views
0

Es ist eine Installer-Klasse innerhalb eines Setupproject, die in einem Winform-Projekt ist. Bis jetzt hatte ich irgendeine Fehlermeldung, sie wurde einfach nicht aufgerufen. Das RunInstallerAttribute wird auf True festgelegt.VS2010 auf Win7, Installer-Klasse in einem Setup heißt nicht

Das einzige, was übrig bleibt, ist die "Hauptlücke", aber ich kann es nicht sagen, weil das für das winformproject nötig ist.

Hier ist der gesamte Code:

using System; 
using System.Collections; 
using System.Diagnostics; 
using System.ComponentModel; 
using System.Configuration.Install; 
using System.Security.AccessControl; 
using System.Security.Principal; 
using System.IO; 
using System.Windows.Forms; 
using System.Text; 
using System.Threading; 

[RunInstaller(true)] 
partial class MyInstaller : Installer 
{ 

    public MyInstaller() 
    {  
     MessageBox.Show("MyInstaller"); 
     InitializeComponent(); 
    } 


    #region "onAfter" 
    protected override void OnAfterInstall(IDictionary savedState) 
    { 

     base.OnAfterInstall(savedState); 
    } 


    protected override void OnAfterRollback(IDictionary savedState) 
    { 

     base.OnAfterRollback(savedState); 
    } 


    protected override void OnAfterUninstall(IDictionary savedState) 
    { 

     base.OnAfterUninstall(savedState); 
    } 
    #endregion 

    #region "OnBefore" 


    protected override void OnBeforeInstall(IDictionary savedState) 
    { 
     base.OnBeforeInstall(savedState); 
    } 


    protected override void OnBeforeRollback(IDictionary savedState) 
    { 

     base.OnBeforeRollback(savedState); 
    } 


    protected override void OnBeforeUninstall(IDictionary savedState) 
    { 

     base.OnBeforeUninstall(savedState); 
    } 
    #endregion 

    #region "OnCommitt" 

    protected override void OnCommitted(IDictionary savedState) 
    { 

     base.OnCommitted(savedState); 
    } 


    protected override void OnCommitting(IDictionary savedState) 
    { 

     base.OnCommitting(savedState); 
    } 
    #endregion 

    #region "Rollback" 
    public override void Rollback(IDictionary savedState) 
    { 
     base.Rollback(savedState); 
     try 
     { 
      string fileName = savedState["myExe"].ToString(); 
      //MsgBox("Rollback ..." & fileName) 
      if (File.Exists(fileName)) 
      { 
       File.Delete(fileName); 
      } 
     } 


     catch (InstallException ex) 
     { 

      MessageBox.Show("Uninstall" + ex.ToString()); 
     } 

     catch (Exception ex) 
     { 
      MessageBox.Show("Uninstall" + ex.ToString()); 
     } 
    } 

    #endregion 

    #region "Uninstall" 
    public override void Uninstall(IDictionary savedState) 
    { 
     try 
     { 
      string fileName = savedState["myExe"].ToString(); 

      if (File.Exists(fileName)) 
      { 
       File.Delete(fileName); 
      } 

      base.Uninstall(savedState); 

     } 
     catch (InstallException ex) 
     { 
      MessageBox.Show("Uninstall" + ex.ToString()); 
     } 

     catch (Exception ex) 
     { 
      MessageBox.Show("Uninstall" + ex.ToString()); 
     } 


    } 

    #endregion 

    #region "Install" 
    public override void Install(IDictionary savedState) 
    { 
     MessageBox.Show("Install "); 
     string strTargetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "myTest");  
     savedState.Add("myExe", strTargetPath);   
     base.Install(savedState); 

    } 
    #endregion 

    #region "Commit" 
    public override void Commit(IDictionary savedState) 
    { 


     string strPath = ""; 

     try 
     { 


      strPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); 
      strPath = Path.Combine(strPath, "myTest\\myApp.exe");   

      using (Process process = new Process()) 
      { 
       process.StartInfo.FileName = "myApp.exe"; 
       process.StartInfo.Arguments = strPath; 
       process.StartInfo.UseShellExecute = false; 
       process.StartInfo.RedirectStandardOutput = true; 
       process.StartInfo.RedirectStandardError = true; 

       StringBuilder output = new StringBuilder(); 
       StringBuilder error = new StringBuilder(); 

       using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false)) 
       using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false)) 
       { 
        process.OutputDataReceived += (sender, e) => 
        { 
         if (e.Data == null) 
         { 
          outputWaitHandle.Set(); 
         } 
         else 
         { 
          output.AppendLine(e.Data); 
         } 
        }; 
        process.ErrorDataReceived += (sender, e) => 
        { 
         if (e.Data == null) 
         { 
          errorWaitHandle.Set(); 
         } 
         else 
         { 
          error.AppendLine(e.Data); 
         } 
        }; 

        process.Start(); 

        process.BeginOutputReadLine(); 
        process.BeginErrorReadLine(); 

        if (process.WaitForExit(1000) && 
         outputWaitHandle.WaitOne(1000) && 
         errorWaitHandle.WaitOne(1000)) 
        { 
         // Process completed. Check process.ExitCode here. 
        } 
        else 
        { 
         // Timed out. 
        } 
       } 
      } 

     } 

     catch (Exception ex) 
     { 
      MessageBox.Show("Commit " + ex.ToString()); 
      Application.Exit();   
     } 

    } 


    #endregion 

} 

Antwort

0

Der wahrscheinlichste Grund ist, dass Sie nicht die Versammlung als eine benutzerdefinierte Aktion im Setup-Projekt hinzugefügt haben, da man nicht so tun, nicht erwähnt.

+0

Hallo Phill, auch ich havnt es zu erwähnen, aber ich habe es so. (Ich habe mich gestern immer wieder auf meine Frage vorbereitet, und schließlich habe ich vergessen, sie hineinzulegen, sogar Es war in dem ganzen Brief) – goran

0

Es funktioniert jetzt. Ich erstelle diese "CommonAppDataFolder", so scheint es in einem C# -Projekt benötigt. Vor Jahren habe ich es mit einem vb.net-Projekt gemacht, kann mich nicht erinnern, dass es nötig war. Es ist notwendig, die App mit diesen speziellen Rechten zu installieren, aber der Aufruf des Installers war nicht davon abhängig.

Und finallay Ich musste den using-Teil "using (Process Prozess = new Process())", innerhalb von "Commit". Denn egal was ich in -filename- und -argument- eingebe, es wird immer ein Fehler geworfen, - dass es den Ordner "C/Program86/... .myApp.exe" nicht finden kann.

Ich war wirklich glücklich, als ich dieses Teil fand, dachte ich, ich habe jetzt, was übrig ist, um das endgültige Installationsfenster zu schließen "Installation abgeschlossen". Ich muss dieses Fenster immer manuell schließen.

Das Verfahren Commit sieht nun wie folgt aus:

public override void Commit(IDictionary savedState) 
    { 
     string strPath = ""; 
     var myProcess = new Process(); 

     try 
     { 
      base.Commit(savedState); 

      strPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Test");   
      strPath = Path.Combine(strPath, "myApp.exe");   
      myProcess.StartInfo.FileName = strPath; 
      myProcess.StartInfo.CreateNoWindow = false; 
      myProcess.Start(); 
      myProcess.WaitForExit(500); 
      myProcess.Close(); 
      myProcess = null;   
     }   

     catch (Exception ex) 
     { 
      MessageBox.Show("public override void Commit " + ex.ToString()); 
      Application.Exit();   
     } 
    }