public GitControl(string fileDirectory)
FileDirectory = fileDirectory;
public string FileDirectory
public string Add(string file = ".")
string str = "add " + file;
private string ConvertDate(DateTime time)
//1. 一月 January (Jan)2. 二月 February (Feb)
//11. 十一月 November (Nov)12. 十二月 December (Dec)
List<string> temp = new List<string>()
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
//StringBuilder str = new StringBuilder();
// git commit --date = "月 日 时间 年 +0800" - am "提交"
//git commit --date = "May 7 9:05:20 2016 +0800" - am "提交"
return $"--date=\"{temp[time.Month - 1]} {time.Day} {time.Hour}:{time.Minute}:{time.Second} {time.Year} +0800\" ";
public string Commit(string str = null, DateTime time = default(DateTime))
string commit = " commit";
if (time != (default(DateTime)))
commit += " " + ConvertDate(time);
if (string.IsNullOrEmpty(str))
if (time == default(DateTime))
str = time.Year + "年" + time.Month + "月" +
time.Minute + ":" + time.Second;
commit += " -m " + "\"" + str + "\"";
//commit = FileStr() + commit;
if (string.IsNullOrEmpty(Branch))
if (string.IsNullOrEmpty(Origin))
string str = $"push {Origin} {Branch}";
string str = $"push {Origin} {Branch}";
str += Control($"push {Origin} {Branch}");
private string _gitStr = "git -C {0}";
return string.Format(_gitStr, FileDirectory) + " ";
private string Control(string str)
// string str = Console.ReadLine();
//System.Console.InputEncoding = System.Text.Encoding.UTF8;//乱码
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true; //重定向标准错误输出
p.StartInfo.CreateNoWindow = true; //不显示程序窗口
p.StartInfo.StandardOutputEncoding= Encoding.UTF8;//Encoding.GetEncoding("GBK");//乱码
p.StandardInput.WriteLine(str + "&exit");
p.StandardInput.AutoFlush = true;
//向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
//同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
string output = p.StandardOutput.ReadToEnd();
output += p.StandardError.ReadToEnd();
p.WaitForExit(); //等待程序执行完退出进程