Vs2005下制作自定义的安装程序(二)

来源:互联网 发布:视频变音软件 编辑:程序博客网 时间:2024/05/23 01:58

下一步我们就需要编写自定义安装的代码了。这里也比较简单,只需要重写Install函数就可以了。

[RunInstaller(true)]

    public partial class MTUSMSInstaller : Installer

    {

        public MTUSMSInstaller()

        {

            InitializeComponent();

        }

        请注意下面的函数,该函数就是重写的函数。这里面我加入了一个函数                  InstallApplication()

        public override void Install(System.Collections.IDictionary stateSaver)

        {

            base.Install(stateSaver);

            InstallApplication();

     }

}

        private void InstallApplication()

        {

            //请注意下面的方法,这里就是完成注册Ocx组件的方法。在这里所有和注册有关的信息我都建立在一个RegisterInstallInfo类里。

            bool _Result=RegisterInstallInfo.RegisterOCXActivex(_TargetDir," SMSCommunication.OCX ");

        }

   public class RegisterInstallInfo

{

 

/// <summary>

        /// 注册Activex控件

        /// </summary>

        /// <param name="SMSOCXPath">这里是指组件的路径</param>

        /// <param name="ActivexName"></param>

        /// <returns></returns>

        public static bool RegisterOCXActivex(string SMSOCXPath,string ActivexName)

        {

            // SMSOCXPath路径就是安装好程序后,安装的程序的路径,该路径如何获得,我会在安装数据库文件里详细说明

            //把控件复制到System32

            bool _Result = false;

           //这里是判断是获得系统的System32的路径,需要把组件复制到里面去

            string _System32Path = GetSystem32Path();

            if (_System32Path != "")

            {

                try

                {

                    //注册短消息控件

                    string _AimPath = _System32Path +"//"+ActivexName;

                    File.Copy(SMSOCXPath + ActivexName, _AimPath,true);

                    Process MyProcess = new Process();

                    MyProcess.StartInfo.FileName = "Regsvr32.exe";

                    MyProcess.StartInfo.Arguments = String.Format("{0} /s", _AimPath);

                    MyProcess.Start();

                    MyProcess.WaitForExit();

                    MyProcess.Close();

                    _Result = true;

 

                }

                catch

                {

                    _Result = false;

                }

            }

            return _Result;

 

        }

}这样安装OCX组件的任务就完成了,不过这里还不能运行,还有其他工作没有做。请继续看下面的内容,所有内容阐述完后你就清楚了。

原创粉丝点击