[C#] 纯文本查看 复制代码protected override void OnAfterInstall(IDictionary savedState)
{
var path = Context.Parameters["tardir"];
_setUpPath = path.Substring(0, path.Length - 2);
#region 安装MySql
//安装MySql
if (!IsExitMySql())//判断MySql服务是否存在
{
CreatePath(_mySqlPath);//创建MySql路径
CopyFile(_setUpPath + "\\MySQL Server 5.5", _mySqlPath);//拷贝文件
InstallMySqlService(_mySqlPath);//注册MySql服务
StartMySqlService();//启动MySql服务
}
#endregion
base.OnAfterInstall(savedState);
}
///
/// 是否存在MySql
///
/// true存在,false不存在
private bool IsExitMySql()
{
ServiceController[] serviceController = System.ServiceProcess.ServiceController.GetServices();//根据服务名称获取目标机器服务
foreach (ServiceController item in serviceController)
{
//判断服务是否存在名为MySQL的服务
if (item.ServiceName == "MySQL")
{
return true;
}
}
return false;
}
///
/// 创建目标目录
/// 目标目录
///
private void CreatePath(string path)
{
//判断目录是否存在
if (!Directory.Exists(path))
{
string[] newPath = path.Split('\\');
string temp = string.Empty;
for (int i = 0; i < newPath.Length; i++)
{
temp += newPath.Trim() + "\\";
//循环创建不存在的目录
if (!Directory.Exists(temp))
Directory.CreateDirectory(temp);
}
}
}
///
/// 拷贝文件到目标目录
/// 源路径
/// 目标路径
///
private void CopyFile(string sourcePath, string targetPath)
{
//如果目标路径不存在则创建目录
if (!Directory.Exists(targetPath))
{
CreatePath(targetPath);
}
string[] files = Directory.GetFiles(sourcePath);
for (int i = 0; i < files.Length; i++)
{
string[] childFiles = files.Split('\\');
File.Copy(files, targetPath + @"\" + childFiles[childFiles.Length - 1], true);
}
string[] directories = Directory.GetDirectories(sourcePath);
for (int i = 0; i < directories.Length; i++)
{
string[] childDirectories = directories.Split('\\');
CopyFile(directories, targetPath + @"\" + childDirectories[childDirectories.Length - 1]);
}
}
///
/// 安装并启动MySql服务
/// MySql路径
///
private void InstallMySqlService(string mySqlPath)
{
try
{
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 + "\\bin");
process.StandardInput.WriteLine("mysqld --install MySQL --defaults-file=" + '"' + mySqlPath + "\\my.ini" + '"');//注册MySQL服务
Thread.Sleep(2000);
process.Dispose();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "注册MySql服务出错");
}
}
///
/// 启动MySql服务
///
private void StartMySqlService()
{
try
{
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();
//启动MySql服务
process.StandardInput.WriteLine("net start mysql");
Thread.Sleep(3000);
process.Dispose();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message,"启动MySql服务出错");
}
}