0

I have a command line process that I would like to call:

process.StartInfo.FileName = "docker"; 
process.StartInfo.Arguments = "--format='{{(index (index .NetworkSettings.Ports \"5000/tcp\") 0).HostPort}}' " + containerId;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = "./";

process.Start();
process.ErrorDataReceived += new DataReceivedEventHandler(ErrorOutputReceived);
process.OutputDataReceived += new DataReceivedEventHandler(OutputReceived);
process.EnableRaisingEvents = true;
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit(); //this limit might need to be changed
process.ErrorDataReceived -= new DataReceivedEventHandler(ErrorOutputReceived);   
process.OutputDataReceived -= new DataReceivedEventHandler(OutputReceived);

Now, if I use the command at the command line, it works. If I call this code, it fails. I have tracked it down to the quotes around "5000/tcp". Dotnet has some fun rules that prevent it from processing this correctly, even though I am escaping them properly. I have tried these rules and they are giving me some problems. Anyone able to tell me what the escaping characters should be?

Here is the link I saw, but it is not working out as they mention: Backslash and quote in command line arguments

supermitsuba
  • 186
  • 7

2 Answers2

1
process.StartInfo.Arguments = 
    @"--format='{{(index (index .NetworkSettings.Ports \""5000/tcp\"") 0).HostPort}}' " + containerId;

Notice @ and double quotes "".

Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
  • 1
    Ha, figures that this is the only combination I haven't tried. This worked for me! Thanks! – supermitsuba Nov 27 '17 at 17:48
  • Nevermind, I misspoke. This doesnt help either. I think its because its a mix of single and double quotes. If you just have one set quotes, its fine. – supermitsuba Nov 28 '17 at 00:34
  • That's strange. How about creating `docker.bat` that contains your .exe call with args and then run that batch via Process? You can pass containerId to batch file call, too. – Mikko Viitala Nov 28 '17 at 16:54
  • Yeah I could probably use a batch file instead, but I thought that it wouldnt scale as well as just calling a command directly. – supermitsuba Dec 01 '17 at 01:45
  • Always write the batch in user temp before running ;) – Mikko Viitala Dec 01 '17 at 17:56
0

I could not get this one to work, but I changed the query:

"--format=\"{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}\""

With this, I will get a list of ports instead. Ill have to process it more, but it gets rid of the single quotes. As long as I have one set of quotes I will be fine.

supermitsuba
  • 186
  • 7