-1

It is supposed to be easy, but I can't get it work.

When I run the following command inside cmd, it works perfectly fine. When I run the command inside the c# code, it won't do anything (no exception has been thrown either).

Here is the command I'm using (which works perfectly fine when using directly on cmd.exe):

cd %ProgramFiles%\PostgreSQL\12\bin && SET PGPASSWORD=mypassword&& pg_restore.exe -U username -d dbname C:\file\to\dump\my.dump

Here is what I'm trying to do in c# (not working):

var arg = @"cd %ProgramFiles%\PostgreSQL\12\bin && SET PGPASSWORD=mypassword&& pg_restore.exe -U username -d dbname C:\file\to\dump\my.dump";
Process.Start("cmd.exe", arg);

Am I missing something? I found a lot of posts concerning c# starting a process, but nothing solved my problem. I also tried to start the process with StartInfo and added the Properties like Arguments, FileName, etc., but it did not work either.

Be aware, the question process.start() arguments is not the same - in my case, I neet to set the system variable (SET PGPASSWORD), since pg_restore has no password argument.

gala_m
  • 37
  • 8
  • Possible duplicate of [process.start() arguments](https://stackoverflow.com/questions/3268022/process-start-arguments), you may need to set the `WorkingDirectory`? – Trevor Oct 24 '19 at 13:18
  • You don't need `cmd` for starters. The application you want to run is `pg_restore`, not `cmd`. – Panagiotis Kanavos Oct 24 '19 at 13:34
  • @Çöđěxěŕ I tryed it with WorkingDirectory, it did not work – gala_m Oct 24 '19 at 13:42
  • @PanagiotisKanavos - No, when I start pg_restore, I cannot set the password argument, that's why i need to set the attribute from cmd – gala_m Oct 24 '19 at 13:43
  • 1
    @gala_m you *don't* need to do that, ProcessStartInfo allows you to set environment variables. `cmd` is nothing more than the console – Panagiotis Kanavos Oct 24 '19 at 13:51

3 Answers3

1

cmd is just the console, it isn't needed to start another process.

The application you want to run is pg_restore.exe in the %ProgramFiles%\PostgreSQL\12\bin folder. You can pass environment variables through ProcessStartInfo.EnvironmentVariables dictionary. I'm not sure if the ProcessStartInfo constructor expands environment variables. You can use Environment.ExpandEnvironmentVariables to ensure the path is correct :

var binFolder=@"%ProgramFiles%\PostgreSQL\12\bin\";
var fullPath=Path.Combine(binFolder,"pg_restore.exe");
var arguments=@"-U username -d dbname C:\file\to\dump\my.dump";

var fullPath=Environment.ExpandEnvironmentVariables(pathToRestore);
var startInfo=new ProcessStartInfo(fullPath,arguments) {
       UseShellExecute =false,
       //Need this to read the output if needed
       RedirectStandardOutput = true;
       //Set if needed
       WorkingDirectory = binFolder
};
startInfo.EnvironmentVariables["PGPASSWORD"]=password;

var process=Process.Start(startInfo);

Console.WriteLine(process.StandardOutput.ReadToEnd());
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • This is not working for me either - Shouldn't `WorkingDirectory` be the bin folder where the .exe is placed anyways? I copied your code and changed the paths - it did nothing, i just ran but did not effect a thing. – gala_m Oct 24 '19 at 14:00
  • @gala_m only if `pg_restore.exe` needs access to other files in the `bin` folder. The full path to the executable is already provided in the `fullPath` variable. I changed the code to use the bin folder as the working directory – Panagiotis Kanavos Oct 24 '19 at 14:05
0

Try this:

var command = "SET PGPASSWORD=mypassword && pg_restore.exe";
var param = "-U username -d dbname C:\file\to\dump\my.dump";
var process = new Process();
var processInfo = new ProcessStartInfo(command, param)
            {
                WorkingDirectory = @"%ProgramFiles%\PostgreSQL\12\bin"
            };
process.StartInfo = processInfo;
process.WaitForExit();
Marcelo Júnior
  • 435
  • 1
  • 5
  • 11
  • 1
    Please expand on your answer for example, where they went wrong and why. It doesn't have to be a full, novel-like solution that details every step, only enough to give sight one needs in order to realize your solution. – Trevor Oct 24 '19 at 13:23
  • This is not working, i get the exception "No process is associated with this object.'" – gala_m Oct 24 '19 at 13:40
0

You need to prefix your argument string with /c so that cmd knows that you're telling it what to do:

var arg = @"/c cd %ProgramFiles%\PostgreSQL\12\bin && SET PGPASSWORD=mypassword&& pg_restore.exe -U username -d dbname C:\file\to\dump\my.dump";
Process.Start("cmd.exe", arg);
Albie
  • 67
  • 6