Win7上的VS2010,未调用安装程序中的安装程序类

时间:2016-04-22 00:53:44

标签: c# installer

它是setupproject中的安装程序类,位于winform项目中。 直到现在我确实有任何错误消息,它只是没有被调用。 RunInstallerAttribute设置为true。

唯一剩下的就是"主要的空白",但我无法说出来,因为这是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

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

现在有效。我创建了那些“CommonAppDataFolder”,它似乎是c#项目所需要的。多年前我用vb.net proejct制作它,不记得它是需要的。它需要安装应用程序,当然还有那些特殊权利,但安装程序的调用并不依赖于它。

最后我必须在“提交”中使用“使用(处理流程=新流程())”来使用部分。无论我把什么放在-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();          
        }
    }