vs自定义安装包的制作

来源:互联网 发布:韩国时时彩助赢软件 编辑:程序博客网 时间:2024/04/29 11:51

vs自定义安装包的制作
1 选中项目-右键- 视图- 文件系统,
然后选中左边列表上的应用程序文件夹-右键-添加-项目输出-主输出-确定


2  选中项目-右键- 视图-自定义操作,
然后选中主输出来自clsInstall(活动)-右键-属性
在CustomActionData栏 中填入:       /targetdir="[TARGETDIR]/"

3
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Windows.Forms;
using System.Diagnostics;

namespace clsInstall
{
    [RunInstaller(true)]
    public partial class YqsInstaller : Installer
    {
  
        const String cntS_SoftCaption = "********软件";
        public YqsInstaller()
        {
            InitializeComponent();
            //安装之前
            this.BeforeInstall += new InstallEventHandler(InstallerTest_BeforeInstall);

            //安装之后
            this.AfterInstall += new InstallEventHandler(InstallerTest_AfterInstall);

            //卸载之前
            this.BeforeUninstall += new InstallEventHandler(InstallerTest_BeforeUninstall);

        }
         
        /// <summary>
        /// 卸载之前
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void InstallerTest_BeforeUninstall(object sender, InstallEventArgs e)
             {
                 string strMsg = "the string variable is used storage error message";
                 string strDir = "";
                 string strCmd = "MySQLServer5.0//bin//mysqld.exe/" -remove mysql/"";

                 strDir = this.Context.Parameters["targetdir"];

                 //停止mysql服务
                 if (!ExecCmd(strDir, "net stop mysql", ref strMsg))
                     MessageBox.Show(strMsg, cntS_SoftCaption);
       
                  //安装Mysql服务
                 if (!ExecCmd(strDir, "/"" + strDir + strCmd, ref strMsg))
                     MessageBox.Show(strMsg, cntS_SoftCaption);
                         
             }

        //安装之前
        void InstallerTest_BeforeInstall(object sender, InstallEventArgs e)
        {
            //     MessageBox.Show("安装之前" + this.Context.Parameters["targetdir"] );

            string strMsg = "the string variable is used storage error message";
            string strDir = "";
            string strCmd = "YqsMySQLServer5.0//bin//mysqld.exe/" --install yqsmysql --defaults-file=/"my.ini/"";

            strDir = this.Context.Parameters["targetdir"];

       
            //安装Mysql服务
            String strPath="";
            strPath =  "/"" + strDir + strCmd ;
            MessageBox.Show(strPath, "按");
            if (!ExecCmd(strDir,strPath, ref strMsg))
                MessageBox.Show("安装mysql" + strMsg, cntS_SoftCaption);
      

            //启动mysql服务
            if (!ExecCmd(strDir, "net start yqsmysql", ref strMsg))
                MessageBox.Show(strMsg, cntS_SoftCaption);

            /*
            //添加快捷方式
           //获得桌面文件夹路径
           string strDestopPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
           IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass();
           IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.WshShortcut)shell.
                   CreateShortcut(@strDestopPath + "//摇钱树网吧管理软件.lnk");

           //shortcut =(IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut();
           shortcut.TargetPath =this.Context.Parameters["targetdir"] + "YQS.exe";
           shortcut.Save();
            */

        }

        /// <summary>
        /// 安装之后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void InstallerTest_AfterInstall(object sender, InstallEventArgs e)
        {
            //  MessageBox.Show("安装之后");

            string strDir = "";

            strDir = this.Context.Parameters["targetdir"];

            System.Diagnostics.ProcessStartInfo psiConfig = new System.Diagnostics.ProcessStartInfo(strDir + "qeroxz.exe");//path即是安装的目录
            System.Diagnostics.Process pConfig = System.Diagnostics.Process.Start(psiConfig);
        }

        /// <summary>
        /// 执行应用程序
        /// </summary>
        /// <param name="workingDirectory">工作目录</param>
        /// <param name="exePath">应用程序路径</param>
        /// <param name="strArg">参数</param>
        /// <param name="strErr"></param>
        /// <returns></returns>
        public static bool ExecExe(string workingDirectory, string exePath, string strArguments, ref string strErr)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = exePath;
            p.StartInfo.Arguments = strArguments;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.Start();
            p.WaitForExit(); //组件无限期的等待关联进程退出

            String strOut;
            strOut = null;
            strOut = p.StandardOutput.ReadToEnd();
            strErr = p.StandardError.ReadToEnd();

            if (0 != p.ExitCode)
            {
                strErr = "ExitCode:" + p.ExitCode;
                return false;
            }

            if ((strOut.Length != 0) || (strErr.Length != 0))
            {
                strErr = "";
                Console.WriteLine(strOut);
                return false;
            }
            else
            {
                Console.WriteLine(strOut);
                return true;
            }
        }

        /// <summary>
        /// 使用cmd执行命令行程序
        /// </summary>
        /// <param name="workingDirectory">工作目录</param>
        /// <param name="command">命令行</param>
        /// <returns>命令执行错误,返回false;命令执行成功,返回true.命令行
        /// 的内容输入错误会导致命令执行失败</returns>
        public static bool ExecCmd(string workingDirectory, string command, ref string strErr)
        {

            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.WorkingDirectory = workingDirectory;
            p.StartInfo.UseShellExecute = false;  //同步
            p.StartInfo.RedirectStandardInput = true; //重定向输入
            p.StartInfo.RedirectStandardOutput = true; //重定向输出
            p.StartInfo.RedirectStandardError = true; //重定义错误输出
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏窗口


            p.Start();
            p.StandardInput.WriteLine(command);
            p.StandardInput.WriteLine("exit");
            //退出cmd

            //获得输出信息
           // strErr = p.StandardOutput.ReadToEnd();

            strErr = p.StandardError.ReadToEnd();
    
            //输出为空表示出错了
            if ("" == strErr)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
    }
}

 

 

二 卸载 在应用程序与文件夹里添加  msiexec.exe(xp下载windows目录下)

然后选中 msiexec.exe ,属性 - 参数 - "-unistall {你的productCode}"