好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装

System ; using System . Collections ; using System . Collections . Generic ; using System . ComponentModel ; using System . Configuration . Install ; using System . Windows . Forms ; using System . IO ; using System . Text ; using System . Diagnostics ; namespace CustomAction { [ RunInstaller ( true )] public partial class MyInstallerClassDll : Installer { public MyInstallerClassDll () { InitializeComponent (); } protected override void OnBeforeInstall ( IDictionary savedState ) { string server = this . Context . Parameters [ "server" ]; string user = this . Context . Parameters [ "user" ]; base . OnBeforeInstall ( savedState ); } public override void Install ( IDictionary stateSaver ) { string installPath = this . Context . Parameters [ "targetdir" ]; string server = this . Context . Parameters [ "server" ]; string user = this . Context . Parameters [ "user" ]; //Mysql的配置文件 IniFile ini = new IniFile ( installPath + @"MySQL\MySQL Server 5.5\my.ini" ); //mysql安装路径 ini . Write ( "mysqld" , "basedir" , installPath + @"MySQL\MySQL Server 5.5\" ); //Mysql数据文件夹 ini . Write ( "mysqld" , "datadir" , installPath + @"MySQL\MySQL Server 5.5\Data\" ); base . Install ( stateSaver ); } protected override void OnAfterInstall ( IDictionary savedState ) { string installPath = this . Context . Parameters [ "targetdir" ]; string mysqlpath = installPath + @"MySQL\MySQL Server 5.5\bin" ; string jrePath = installPath + @"Java\jre6\bin" ; string iniPath = installPath + @"MySQL\MySQL Server 5.5\my.ini" ; string tomcatPath = installPath + @"Tomcat\Tomcat6\bin" ; InstallMysql ( mysqlpath , iniPath , true ); //设置Mysql环境变量 SysEnvironment . SetPath ( mysqlpath ); //设置JRE环境变量 SysEnvironment . SetPath ( jrePath ); //设置Tomcat环境变量 SysEnvironment . SetPath ( tomcatPath ); base . OnAfterInstall ( savedState ); } /// <summary> /// 安装与卸载Mysql服务 /// </summary> /// <param name="mysqlpath"></param> /// <param name="iniPath"></param> /// <param name="isInstall"> 为true时安装,否则为卸载 </param> private static void InstallMysql ( string mysqlpath , string iniPath , bool isInstall ) { //安装Mysql服务 Process process = new Process (); process . StartInfo . FileName = "cmd.exe" ; process . StartInfo . UseShellExecute = false ; process . StartInfo . RedirectStandardInput = true ; process . StartInfo . RedirectStandardOutput = true ; process . StartInfo . RedirectStandardError = true ; process . StartInfo . CreateNoWindow = true ; process . Start (); process . StandardInput . WriteLine ( "" ); process . StandardInput . WriteLine ( "cd " + mysqlpath ); process . StandardInput . WriteLine ( mysqlpath . Substring (2) + " cd" ); //mysqld --install MySQLXY --defaults-file="D:\Program Files\MySQL\MySQL Server 5.5\my.ini" if ( isInstall ) { process . StandardInput . WriteLine ( "mysqld --install MySQL --defaults-file=" + ‘"‘ + iniPath + ‘"‘ ); process . StandardInput . WriteLine ( "net start mysql" ); } else { process . StandardInput . WriteLine ( "net stop mysql" ); //mysqld --install MySQLXY --defaults-file="D:\Program Files\MySQL\MySQL Server 5.5\my.ini" process . StandardInput . WriteLine ( "mysqld --remove MySQL --defaults-file=" + ‘"‘ + iniPath + ‘"‘ ); } process . StandardInput . WriteLine ( "" ); process . StandardInput . WriteLine ( "" ); process . StandardInput . WriteLine ( "exit" ); //Writefile(installPath,process.StandardOutput.ReadToEnd().ToString()); process . Close (); } public override void Uninstall ( IDictionary savedState ) { string installPath = this . Context . Parameters [ "targetdir" ]; string mysqlpath = installPath + @"MySQL\MySQL Server 5.5\bin" ; string iniPath = installPath + @"MySQL\MySQL Server 5.5\my.ini" ; InstallMysql ( mysqlpath , iniPath , true ); base . Uninstall ( savedState ); } /// <summary> /// 写日志 /// </summary> /// <param name="path"></param> /// <param name="msg"></param> public void Writefile ( string path , string msg ) { string file = path + @"日志.txt" ; if ( File . Exists ( file )) File . Delete ( file ); using ( StreamWriter sw = new StreamWriter ( file , true )) { sw . WriteLine ( msg ); } } } }

下面是 SysEnvironment 类的代码,读取设置注册表的信息 代码已经封装好了,就不介绍了

(环境变量的注册表位置为HKEY_LOCAL_MACHINE/SYSTEM/ControlSet001 / Session Manager/ Environment )

 using   System ;
 using   System . Collections . Generic ;
 using   System . Text ;
 using   Microsoft . Win32 ;

 namespace   CustomAction
 {
     class   SysEnvironment
     {
         /// <summary>
        ///   获取系统环境变量
          /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
          public static string   GetSysEnvironmentByName ( string   name )
        {
             string   result  =  string . Empty ;
             try
             {
                 result  =  OpenSysEnvironment (). GetValue ( name ). ToString (); //读取
             }
             catch  ( Exception )
            {

                 return string . Empty ;
            }
             return   result ;

        }

         /// <summary>
        ///   打开系统环境变量注册表
          /// </summary>
        /// <returns>  RegistryKey  </returns>
          private static   RegistryKey   OpenSysEnvironment ()
        {
             RegistryKey   regLocalMachine  =  Registry . LocalMachine ;
             RegistryKey   regSYSTEM  =  regLocalMachine . OpenSubKey ( "SYSTEM" ,  true ); //打开HKEY_LOCAL_MACHINE下的SYSTEM 
              RegistryKey   regControlSet001  =  regSYSTEM . OpenSubKey ( "ControlSet001" ,  true ); //打开ControlSet001 
              RegistryKey   regControl  =  regControlSet001 . OpenSubKey ( "Control" ,  true ); //打开Control 
              RegistryKey   regManager  =  regControl . OpenSubKey ( "Session Manager" ,  true ); //打开Control 

              RegistryKey   regEnvironment  =  regManager . OpenSubKey ( "Environment" ,  true );
             return   regEnvironment ;
        }

         /// <summary>
        ///   设置系统环境变量
          /// </summary>
        /// <param name="name">  变量名  </param>
        /// <param name="strValue">  值  </param>
          public static void   SetSysEnvironment ( string   name ,  string   strValue )
        {
             OpenSysEnvironment (). SetValue ( name ,  strValue );

        }

         /// <summary>
        ///   检测系统环境变量是否存在
          /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
          public bool   CheckSysEnvironmentExist ( string   name )
        {
             if  (! string . IsNullOrEmpty ( GetSysEnvironmentByName ( name )))
                 return true ;
             else
                return false ;
        }

         /// <summary>
        ///   添加到PATH环境变量(会检测路径是否存在,存在就不重复)
          /// </summary>
        /// <param name="strPath"></param>
          public static void   SetPath ( string   strHome )
        {
             string   pathlist  =  GetSysEnvironmentByName ( "PATH" );
             string []  list  =  pathlist . Split ( ‘;‘ );
             bool   isPathExist  =  false ;

             foreach  ( string   item   in   list )
            {
                 if  ( item  ==  strHome )
                     isPathExist  =  true ;
            }
             if  (! isPathExist )
            {
                 SetSysEnvironment ( "PATH" ,  pathlist  + strHome +  ";" );
            }

        }
    }
}

好了,接下来创建Ini文件操作类,调用了系统api,已经封装好了,可以直接用了

 using   System ;
 using   System . IO ;
 using   System . Collections . Generic ;
 using   System . Text ;
 using   System . Runtime . InteropServices ;

 namespace   CustomAction
 {
     public class   IniFile
     {
         private string   m_iniFileFullPath ;

         /// <summary>
        ///   ini文件路径
          /// </summary>
        /// <param name="iniFilePath"></param>
          public   IniFile ( string   iniFilePath )
        {
             m_iniFileFullPath  =  iniFilePath ;
        }

         /// <summary>
        ///   写入信息
          /// </summary>
        /// <param name="iniSection"></param>
        /// <param name="iniKey"></param>
        /// <param name="iniValue"></param>
          public void   Write ( string   iniSection ,  string   iniKey ,  string   iniValue )
        {
             WritePrivateProfileString ( iniSection ,  iniKey ,  iniValue ,  this . m_iniFileFullPath );
        }

         /// <summary>
        ///   读取信息
          /// </summary>
        /// <param name="iniSection"></param>
        /// <param name="iniKey"></param>
        /// <returns></returns>
          public string   Read ( string   iniSection ,  string   iniKey )
        {
             StringBuilder   resultValue  =  new   StringBuilder (255);
             int   i  =  GetPrivateProfileString ( iniSection ,  iniKey ,  "" ,  resultValue ,
                                            255,  this . m_iniFileFullPath );
             return   resultValue . ToString ();
        }

         /// <summary>
        ///   写入信息
          /// </summary>
        /// <param name="iniSection"></param>
        /// <param name="iniKey"></param>
        /// <param name="iniValue"></param>
        /// <param name="iniPath"></param>
          public void   Write ( string   iniSection ,  string   iniKey ,  string   iniValue ,  string   iniPath )
        {
             WritePrivateProfileString ( iniSection ,  iniKey ,  iniValue ,  iniPath );
        }

         /// <summary>
        ///   读取信息
          /// </summary>
        /// <param name="iniSection"></param>
        /// <param name="iniKey"></param>
        /// <param name="iniPath"></param>
        /// <returns></returns>
          public static string   Read ( string   iniSection ,  string   iniKey ,  string   iniPath )
        {
             StringBuilder   resultValue  =  new   StringBuilder (255);
             int   i  =  GetPrivateProfileString ( iniSection ,  iniKey ,  "" ,  resultValue ,
                                            255,  iniPath );
             return   resultValue . ToString ();
        }

        [ DllImport ( "kernel32" )]
         private static extern long   WritePrivateProfileString ( string   section ,
             string   key ,  string   val ,  string   filePath );

        [ DllImport ( "kernel32" )]
         private static extern int   GetPrivateProfileString ( string   section ,
                  string   key ,  string   def ,  StringBuilder   retVal ,
             int   size ,  string   filePath );
    }
}

现在基本代码已经写好了,右键解决方案,添加安装部署项目,右键项目文件视图,新建文件夹Mysql、Java、Tomcat,将你的拷贝的Mysql贴进Mysql项目里面

 

接下来,右键应用程序文件夹添加主输出

然后右键项目,自定义操作视图,添加安装和卸载的操作(选中主输出)

好了,现在可以测试下了Mysql,未完待续。。。。。。。

下次接着写Tomcat

C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装

标签:

查看更多关于C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装的详细内容...

  阅读:38次