-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
72 lines (68 loc) · 2.42 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Diagnostics;
using System.IO;
namespace mysqlbackup
{
class Program
{
static void Main(string[] args)
{
Console.Out.WriteLine("MySQL/MariaDB Backup");
mysql setting = mysql.Default;
DateTime now = DateTime.Now;
string datatime = now.ToString("yyyyMMddhhmmss");
string strCmd = string.Format("{0} -u{1} -p{2} {3}>{4}", setting.mysql_pathname,
setting.mysql_username, setting.mysql_password, setting.mysql_database,(setting.mysql_database + "-" + datatime) +".sql.bak");
string echo = new Program().StartCmd(".\\", strCmd);
if ("".Equals(echo))
{
echo = "Database " + setting.mysql_database + " has been successfully exported";
}
Console.Out.WriteLine(echo);
}
/// <summary>
/// Run command line
/// </summary>
/// <param name="workingDirectory">working directory</param>
/// <param name="command">commang</param>
public string StartCmd(string workingDirectory, string command)
{
string strOutput = "";
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C" + command;
p.StartInfo.WorkingDirectory = workingDirectory;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
try
{
if (p.Start())
{
StreamReader reader = p.StandardOutput;
StreamReader error = p.StandardError;
strOutput = reader.ReadToEnd();
if (error != null)
{
Console.Error.WriteLine(error.ReadToEnd());
} else
{
Console.WriteLine(reader.ReadToEnd());
}
p.WaitForExit();
}
}
catch (Exception exp)
{
Console.Error.WriteLine(exp.Message);
}
finally
{
if (p != null) p.Close();
}
return strOutput;
}
}
}