设为首页 收藏本站
查看: 592|回复: 0

[经验分享] C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装

[复制链接]

尚未签到

发表于 2015-6-19 10:34:19 | 显示全部楼层 |阅读模式
  要求:


  • 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);
}
///
/// 安装与卸载Mysql服务
///
///
///
/// 为true时安装,否则为卸载
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);
}
///
/// 写日志
///
///
///
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
{
///
/// 获取系统环境变量
///
///
///
public static string GetSysEnvironmentByName(string name)
{
string result = string.Empty;
try
{
result = OpenSysEnvironment().GetValue(name).ToString();//读取
}
catch (Exception)
{
return string.Empty;
}
return result;
}
///
/// 打开系统环境变量注册表
///
/// RegistryKey
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;
}
///
/// 设置系统环境变量
///
/// 变量名
/// 值
public static void SetSysEnvironment(string name, string strValue)
{
OpenSysEnvironment().SetValue(name, strValue);
}
///
/// 检测系统环境变量是否存在
///
///
///
public bool CheckSysEnvironmentExist(string name)
{
if (!string.IsNullOrEmpty(GetSysEnvironmentByName(name)))
return true;
else
return false;
}
///
/// 添加到PATH环境变量(会检测路径是否存在,存在就不重复)
///
///
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;
///
/// ini文件路径
///
///
public IniFile(string iniFilePath)
{
m_iniFileFullPath = iniFilePath;
}
///
/// 写入信息
///
///
///
///
public void Write(string iniSection, string iniKey, string iniValue)
{
WritePrivateProfileString(iniSection, iniKey, iniValue, this.m_iniFileFullPath);
}
///
/// 读取信息
///
///
///
///
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();
}
///
/// 写入信息
///
///
///
///
///
public void Write(string iniSection, string iniKey, string iniValue, string iniPath)
{
WritePrivateProfileString(iniSection, iniKey, iniValue, iniPath);
}
///
/// 读取信息
///
///
///
///
///
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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-78823-1-1.html 上篇帖子: mysql 2013错误 下篇帖子: 【原创】使用 Rotate Master 实现MySQL 多主复制
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表