using Apache.NMS.ActiveMQ.Commands; using Rhea.Common; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Tiger.IBusiness; namespace Tiger.Business { public class DbBackup : IDbBackup { /// /// 运行备份数据库bat文件 /// /// /// public async Task ExcuteBackupBat(string path) { var action = new ApiAction(); if (!File.Exists(path)) { //Console.WriteLine("文件不存在!"); return null; } StringBuilder builder = new StringBuilder(); try { Process process = new Process(); FileInfo fileInfo = new FileInfo(path); process.StartInfo.WorkingDirectory = fileInfo.Directory.FullName; //关闭 shell 文件路径不一样 process.StartInfo.FileName = path; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; //重定向输入 process.StartInfo.RedirectStandardOutput = true; //重定向标准输出 process.StartInfo.RedirectStandardError = true; //重定向错误输出 process.StartInfo.CreateNoWindow = true; process.Start(); // StreamReader reader = process.StandardOutput; string line = "";//每次读取一行 while ((line = reader.ReadLine()) != null) { //Console.WriteLine("message:" + line); builder.AppendLine(line); } process.WaitForExit(); process.Close(); reader.Close(); action.Message = builder.ToString(); Logger.Interface.Info($"执行备份数据库批处理脚本信息: {builder}"); } catch (Exception ex) { action.CatchExceptionWithLog(ex, $"执行备份数据库批处理脚本出错"); } return action; } } }