2016-04-22 79 views
0

它是setupproject中的安裝程序類,它位於winform項目中。直到現在,我確實擁有任何errro消息,它只是不叫。 RunInstallerAttribute設置爲true。Win7上的VS2010,安裝程序中的安裝程序類不叫

剩下的唯一東西就是「主要空白」,但我不能說出來,導致winformproject是neede。

這裏是整個代碼:

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 

} 

回答

0

最可能的原因是你沒有添加組件的安裝項目自定義操作,因爲你沒有提到這樣做。

+0

嗨菲爾,以及我havnt提及,但我做到了這樣。 (我昨天一次又一次地準備了我的問題,最後我忘了把它放進去,甚至是在整封信裏面) – goran

0

它現在有效。我創建了那些「CommonAppDataFolder」,看起來是它在c#項目中所需要的。幾年前,我用vb.net proejct做了它,不記得它是需要的。它需要安裝該應用程序,當然還有這些特權,但安裝程序的調用並不取決於它。

And finallay我必須在「Commit」中使用部分「using(Process process = new Process())」。原因不管我給-filename-和 -argument-,它總會拋出一個錯誤, - 它無法找到文件夾「C/Program86/... .myApp.exe」。

當我發現使用部分時,我非常高興,因爲我現在得到了什麼來關閉最終安裝窗口「安裝完成」。我總是需要手動關閉這個窗口。

提交程序現在看起來是這樣的:

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();   
     } 
    } 
相關問題