using System.Threading.Tasks;
/// <param name="args"></param>
static void Main(string[] args)
bool march = ReadCustomAppini();
string str = System.AppDomain.CurrentDomain.BaseDirectory + "gogs.exe web";
System.Diagnostics.Process p = new System.Diagnostics.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.StandardInput.WriteLine(str + "&exit");
p.StandardInput.AutoFlush = true;
//p.StandardInput.WriteLine("exit");
//向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
//同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
p.OutputDataReceived += (s, e) =>
Console.Write(e.Data + "\r\n");
p.ErrorDataReceived += (s, e) =>
Console.Write(e.Data + "\r\n");
Console.WriteLine(p.StandardOutput.ReadLine());
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();//等待程序执行完退出进程
Console.WriteLine(output);
private static void Restore()
string file = System.AppDomain.CurrentDomain.BaseDirectory + "custom\\conf\\app.ini";
var ini = new FileIniDataParser();
IniData customAppini = ini.ReadFile(file);
string gogs = System.AppDomain.CurrentDomain.BaseDirectory;
customAppini["repository"]["ROOT"] = gogs.Replace("\\", "/");
gogs = System.AppDomain.CurrentDomain.BaseDirectory + "log";
customAppini["log"]["ROOT_PATH"] = gogs.Replace("\\", "/");
using (StreamWriter stream = new StreamWriter(file))
ini.WriteData(stream, customAppini);
private static void RestoreUpdate()
var folder = new DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory);
var gogs = System.AppDomain.CurrentDomain.BaseDirectory + "gogs.exe";
var gc = System.AppDomain.CurrentDomain.BaseDirectory + "custom\\conf\\app.ini";
gogs = gogs.Replace("\\", "/");
gc = gc.Replace("\\", "/");
foreach (var temp in folder.EnumerateDirectories("*git", SearchOption.AllDirectories))
var file = temp.FullName + "\\hooks" + "\\update";
string str = $"#!/usr/bin/env bash\n\"{gogs}\" update $1 $2 $3 --config='{gc}'";
using (StreamWriter stream = new StreamWriter(file))
private static bool ReadCustomAppini()
string file = System.AppDomain.CurrentDomain.BaseDirectory;
if (!file.EndsWith("\\"))
file += "custom\\conf\\app.ini";
var ini = new FileIniDataParser();
IniData customAppini = ini.ReadFile(file);
var gogs = customAppini["repository"]["ROOT"];
gogs = gogs.Replace("/", "\\");
if (!gogs.EndsWith("\\"))
return gogs != AppDomain.CurrentDomain.BaseDirectory;