简体   繁体   中英

VS2010 on Win7, installer class within a setup is not called

It is a installer class within a setupproject, which is within an winform project. Till now I did not have any error message, its just not called. The RunInstallerAttribute is set to true.

The only thing which is left is the "main void", but I can't put it, cause the is needed for the winformproject.

Here is the entire 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
   
}

最可能的原因是您没有在安装项目中将程序集添加为自定义操作,因为您没有提到这样做。

It works now. I create those „CommonAppDataFolder“ , seem so its needed in ac# project. Years ago I made it with a vb.net proejct , cant remember it were needed. Its needde to install the app, with those speacial rights of course, but the call of the installer was not depend on it.

And finallay I had to get out the using part „using (Process process = new Process())“, within „Commit“. Cause no matter what I put to -filename- and -argument-, it always throw an error, - that it cant find the folder „C/Program86/….myApp.exe“.

I was reallly happy when I found this using part, thought I got now whats left to close the final installattion window „Installation complete“. I always have to close this window manually.

The Commit procedure looks now like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM