Mysql用的第三方软件是啥_用WindowsInstaller打包第三方软件如MySql

[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服务出错");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值