要求:
- JDK、Mysql、Tomcat三者制作成一個安裝包,
- 不能單獨安裝,安裝過程不顯示三者的界面,
- 安裝完成要配置好JDK環境、Mysql服務、Tomcat 服務
目的:
- 解決客戶在安裝軟件的復雜配置和繁瑣
- 便於管理軟件版本
- 便於系統集成
分析:
由於不能使用軟件的原始安裝版本,故只能將JDK的安裝目錄拷貝出來,放在D盤的SoftSource文件夾,由於要管理三者,將這三個放進一個文件夾里面
Mysql、Tomcat只能用解壓版,要讓軟件運行起來,要做的事情如下:
- 配置JDK環境變量
這一步很重要,否則后面的Tomcat將不能正確運行,
2、安裝Mysql服務,我用的是MySQL Server 5.5社區版、解壓目錄下面有my.ini文件,或者先將mysql安裝,然后拷貝安裝目錄文件,目錄結構不能變,安裝方法是 用命令行將目錄轉到mysql的bin目錄下,mysqld --install MySQL5 --defaults-file="C:\Program Files\MySQL\MySQL Server 5.5\my.ini" 注意=后面就是你的mysql安裝目錄下的ini文件路徑(可先行在cmd下測試安裝,若可以,刪除服務的只要將前面的安裝語句里面的install改成uninstall)
難點在my.ini文件里面的datadir="D:/ProgramData/MySQL/MySQL Server 5.5/Data/"(數據安裝目錄)
basedir="D:/Program Files/MySQL/MySQL Server 5.5/" (軟件安裝目錄)
需要與你的實際目錄對應,否則會失敗,另外要根據選擇不同路徑,該路徑要可以跟着改變
3、安裝Tomcat服務是要執行bin目錄下面的service.bat文件用法命令行將目錄的轉到你的Tomcat 下的bin目錄安裝語句是
service.bat install 卸載 為service.bat uninstall 主要事項(bat文件必須要在當前目錄執行,就是命令行的路徑必須要轉到bat文件的目錄,另外需要JAVA_HOME環境變量,還有CATALINA_HOME環境變量(就是要將Tomcat安裝目錄加到環境變量))
思路清晰了,接着就是代碼實現了,(先實現JDK和Mysql )
vs2008 新建 C#類庫項目,添加組件Installer1.cs(安裝組件)命名為MyInstallerClassDll
重寫安裝方法(安裝前、安裝、安裝后)附上代碼:
using 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
