public void btnBackup_Click(object sender, EventArgs e)
{
BackupDatabase(
"localhost",
"5432",
"postgres",
"***********",
"PerfectFreight",
txtPath.Text,
"Backup #",
"C:\Program Files\PostgreSQL\13\bin\");
}
public static string BackupDatabase(
string server,
string port,
string user,
string password,
string dbname,
string backupdir,
string backupFileName,
string backupCommandDir)
{
try
{
Environment.SetEnvironmentVariable("PGPASSWORD", password);
string backupFile = backupdir + backupFileName + DateTime.Now.ToString("MM") + "_" + DateTime.Now.ToString("dd") + "_" + DateTime.Now.ToString("yyyy") + "_Time?" + DateTime.Now.ToString("HH") + ";" + DateTime.Now.ToString("mm") + ".backup";
string BackupString = " -U " + user + " -h " + server + " -p " + port + " -d " + dbname + " -F c -b -v -f "" + backupFile;
Process proc = new Process();
proc.StartInfo.FileName = backupCommandDir + "\pg_dump.exe";
proc.StartInfo.Arguments = BackupString;
proc.StartInfo.RedirectStandardOutput = true;//for error checks BackupString
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;//use for not opening cmd screen
proc.StartInfo.CreateNoWindow = true;//use for not opening cmd screen
proc.Start();
proc.WaitForExit();
proc.Close();
MessageBox.Show("Backup successfully created");
return backupFile;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
The program starts to run but does not finish, it does not give an error. In the instruction proc.WaitForExit();
the execution gets stuck as in an infinite loop.
question from:
https://stackoverflow.com/questions/66056680/waitforexit-remains-dont-finish 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…