要求:
目的:
分析:
由于不能使用軟件的原始安裝版本,故只能將JDK的安裝目錄拷貝出來,放在D盤的SoftSource文件夾,由于要管理三者,將這三個(gè)放進(jìn)一個(gè)文件夾里面
Mysql、Tomcat只能用解壓版,要讓軟件運(yùn)行起來,要做的事情如下:
這一步很重要,否則后面的Tomcat將不能正確運(yùn)行,
2、安裝Mysql服務(wù),我用的是MySQL Server 5.5社區(qū)版、解壓目錄下面有my.ini文件,或者先將mysql安裝,然后拷貝安裝目錄文件,目錄結(jié)構(gòu)不能變,安裝方法是 用命令行將目錄轉(zhuǎn)到mysql的bin目錄下,mysqld --install MySQL5 --defaults-file="C:\Program Files\MySQL\MySQL Server 5.5\my.ini" 注意=后面就是你的mysql安裝目錄下的ini文件路徑(可先行在cmd下測試安裝,若可以,刪除服務(wù)的只要將前面的安裝語句里面的install改成uninstall)
難點(diǎn)在my.ini文件里面的datadir="D:/ProgramData/MySQL/MySQL Server 5.5/Data/"(數(shù)據(jù)安裝目錄)
basedir="D:/Program Files/MySQL/MySQL Server 5.5/" (軟件安裝目錄)
需要與你的實(shí)際目錄對應(yīng),否則會失敗,另外要根據(jù)選擇不同路徑,該路徑要可以跟著改變
3、安裝Tomcat服務(wù)是要執(zhí)行bin目錄下面的service.bat文件用法命令行將目錄的轉(zhuǎn)到你的Tomcat 下的bin目錄安裝語句是
service.bat install 卸載 為service.bat uninstall 主要事項(xiàng)(bat文件必須要在當(dāng)前目錄執(zhí)行,就是命令行的路徑必須要轉(zhuǎn)到bat文件的目錄,另外需要JAVA_HOME環(huán)境變量,還有CATALINA_HOME環(huán)境變量(就是要將Tomcat安裝目錄加到環(huán)境變量))
思路清晰了,接著就是代碼實(shí)現(xiàn)了,(先實(shí)現(xiàn)JDK和Mysql )
vs2008 新建 C#類庫項(xiàng)目,添加組件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數(shù)據(jù)文件夾 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); //設(shè)置Mysql環(huán)境變量 SysEnvironment.SetPath(mysqlpath); //設(shè)置JRE環(huán)境變量 SysEnvironment.SetPath(jrePath); //設(shè)置Tomcat環(huán)境變量 SysEnvironment.SetPath(tomcatPath); base.OnAfterInstall(savedState); } /// <summary> /// 安裝與卸載Mysql服務(wù) /// </summary> /// <param name="mysqlpath"></param> /// <param name="iniPath"></param> /// <param name="isInstall">為true時(shí)安裝,否則為卸載</param> private static void InstallMysql(string mysqlpath, string iniPath, bool isInstall) { //安裝Mysql服務(wù) 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 類的代碼,讀取設(shè)置注冊表的信息 代碼已經(jīng)封裝好了,就不介紹了
(環(huán)境變量的注冊表位置為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> /// 獲取系統(tǒng)環(huán)境變量 /// </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> /// 打開系統(tǒng)環(huán)境變量注冊表 /// </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> /// 設(shè)置系統(tǒng)環(huán)境變量 /// </summary> /// <param name="name">變量名</param> /// <param name="strValue">值</param> public static void SetSysEnvironment(string name, string strValue) { OpenSysEnvironment().SetValue(name, strValue); } /// <summary> /// 檢測系統(tǒng)環(huán)境變量是否存在 /// </summary> /// <param name="name"></param> /// <returns></returns> public bool CheckSysEnvironmentExist(string name) { if (!string.IsNullOrEmpty(GetSysEnvironmentByName(name))) return true; else return false; } /// <summary> /// 添加到PATH環(huán)境變量(會檢測路徑是否存在,存在就不重復(fù)) /// </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+ ";"); } } }}
好了,接下來創(chuàng)建Ini文件操作類,調(diào)用了系統(tǒng)api,已經(jīng)封裝好了,可以直接用了
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); }}
現(xiàn)在基本代碼已經(jīng)寫好了,右鍵解決方案,添加安裝部署項(xiàng)目,右鍵項(xiàng)目文件視圖,新建文件夾Mysql、Java、Tomcat,將你的拷貝的Mysql貼進(jìn)Mysql項(xiàng)目里面
接下來,右鍵應(yīng)用程序文件夾添加主輸出
然后右鍵項(xiàng)目,自定義操作視圖,添加安裝和卸載的操作(選中主輸出)
好了,現(xiàn)在可以測試下了Mysql,未完待續(xù)。。。。。。。
下次接著寫Tomcat