我在C#中使用System.Diagnostics.Process来调用PostgreSQL备份数据库组件pg_dump.exe。这是我的代码:
string argbackup = @"--host localhost --port 5432 --username ""postgres"" --no-password --format custom --blobs --verbose --file dtname dtname";
Process backupProcess = new Process();
backupProcess.StartInfo.FileName = @"C:/Program Files/PostgreSQL/9.5/bin/pg_dump.exe";
backupProcess.StartInfo.Arguments = argbackup;
backupProcess.EnableRaisingEvents = true;
backupProcess.Exited += (object sender, EventArgs args) =>
{
//Execution after the process ends.
};
backupProcess.Start();
代码可以在C#中成功运行。但是,当我在Visual Studio中发布项目并将其部署在IIS上时,无法调用pg_dum.exe。并且在流程成功结束后运行的代码已运行。
我修改了上面的代码:
var pinfo = new ProcessStartInfo();
pinfo.Arguments = @"--host localhost --port 5432 --username ""postgres"" --no-password --format custom --blobs --verbose --file dtname dtname";
info.FileName = @"C:/Program Files/PostgreSQL/9.5/bin/pg_dump.exe";
pinfo.UseShellExecute = false;
using (var process = new Process())
{
process.EnableRaisingEvents = true;
process.StartInfo = pinfo;
process.Start();
while (!process.HasExited)
Thread.Sleep(10000);
process.WaitForExit();
if (process.ExitCode != 0)
throw new Exception("error");
process.Close();
}
它也可以在C#中使用,但它仍然无法在IIS中运行。
我再次修改了代码:
string argpostgre = @"c: && cd C:/Program Files/PostgreSQL/9.5/bin";
string argbackup = @"pg_dump.exe --host localhost --port 5432 --username ""postgres"" --no-password --format custom --blobs --verbose --file dtname dtname";
Process theProcess = new Process();
theProcess.StartInfo.FileName = @"cmd.exe";
theProcess.StartInfo.UseShellExecute = false;
theProcess.StartInfo.RedirectStandardInput = true;
theProcess.StartInfo.RedirectStandardOutput = true;
theProcess.StartInfo.RedirectStandardError = true;
theProcess.StartInfo.CreateNoWindow = false;
theProcess.EnableRaisingEvents = true;
theProcess.Exited += (object sender, EventArgs args) =>
{
//Execution after the process ends.
};
string strOutput = null;
try
{
theProcess.Start();
theProcess.StandardInput.WriteLine(argpostgre + " && " + argbackup + " & exit");
theProcess.WaitForExit();
}
catch (Exception ex)
{
strOutput = ex.Message;
}
它已在C#中运行并且不会退出。但是在强行关闭C#程序后,可以成功备份数据库。并且它将自动停止进程,但是当我部署到IIS时情况与上面相同。
我确定这不是IIS中的安装问题,因为我还使用了Process组件来访问可以成功运行的WinRAR.exe(C:\ Program Files \ WinRAR \ WinRAR.exe)压缩程序。
我想知道PostgreSQL是否具有权限,因此我无法访问pg_dump.exe和其他组件,例如pg_restore.exe。