访问被拒绝cmd C#

时间:2018-02-09 13:23:16

标签: c# postgresql cmd process

我试图在我的C#项目中制作备份触发器,并且我坚持执行命令。我可以手动执行此操作并且与此处相同,但不知何故,当我尝试在C#项目中实现它时,我无法运行它(可以但是它说“访问被拒绝”)。

string cmdKomanda = @"/k cd C:\Program Files\PostgreSQL\9.6\bin" + Environment.CurrentDirectory+ @"pg_dump -h 192.168.130.240 -p 5433 -U postgres  -F c  postgres > ""C:\Users\Marko Petričević\Documents\Radni_sati_Backup\radni_sati_15_01_18"" " + Environment.CurrentDirectory + @"password";
        ProcessStartInfo procStartInfo =new ProcessStartInfo("CMD.exe", cmdKomanda);
        Process.Start(procStartInfo);

基本上我首先要来PostgreSQL bin文件夹从那里运行pg_dumb。运行该命令后,需要输入密码。

cmdKomanda的运行时值为:

"/k cd C:\\Program Files\\PostgreSQL\\9.6\\binC:\\Users\\Marko Petričević\\Desktop\\TortoiseSVN\\Radni_sati-Dev\\Radni_sati\\bin\\Debugpg_dump -h 192.168.130.240 -p 5433 -U postgres  -F c  postgres > C:\\\\Users\\\\Marko Petričević\\\\Documents\\\\Radni_sati_Backup\\\\radni_sati_15_01_18 C:\\Users\\Marko Petričević\\Desktop\\TortoiseSVN\\Radni_sati-Dev\\Radni_sati\\bin\\Debugpassword"

如果有人知道如何使这项工作,请=)

2 个答案:

答案 0 :(得分:0)

使用cd设施设置工作文件夹以传递ProcessStartInfo,而不是尝试使用PGPASSWORD模拟交互式会话并输入密码:

var cmdKomanda = @"-h 192.168.130.240 -p 5433 -U postgres  -F c  postgres > ""C:\Users\Marko Petričević\Documents\Radni_sati_Backup\radni_sati_15_01_18""";
ProcessStartInfo procStartInfo =new ProcessStartInfo("pg_dump.exe", cmdKomanda);
procStartInfo.WorkingDirectory = @"C:\Program Files\PostgreSQL\9.6\bin";
procStartInfo.EnvironmentVariables["PGPASSWORD"] = password; // pg_dump will pick it up

答案 1 :(得分:0)

找到一个解决方案,看起来我不能使用pg_dump.exe,并且命令必须以/ k开头

 var cmdKomanda = @"/k pg_dump -h 192.168.130.240 -p 5433 -U postgres  -F c  postgres >""C:\Users\Marko Petričević\Documents\Radni_sati_Backup\radni_sati_proba"" ";
        ProcessStartInfo procStartInfo = new ProcessStartInfo("CMD.exe", cmdKomanda);
        procStartInfo.WorkingDirectory = @"C:\Program Files\PostgreSQL\9.6\bin";
        procStartInfo.EnvironmentVariables["PGPASSWORD"] = sifra; // pg_dump 
        procStartInfo.UseShellExecute = false;
        Process.Start(procStartInfo);