-1

The command I'm trying to execute is

"C:\Program Files (x86)\MyAPP\solr-6.2.0\bin\solr" start -f -c -z "10.195.42.93:2181,10.195.42.92:2181" -h 10.195.42.92

And this works just fine on the command Line.

I'm trying to execute this as C# process.

NOTE: Below code works fine if i remove the quotes surrounding IPList

var IPList="10.195.42.93:2181,10.195.42.92:2181";
var hostIP="10.195.42.92"

string command = @"/c ""C:\Program Files (x86)\MyApp\solr-6.2.0\bin\solr"" start -f -c -z """ + IPList + @""" -h " + hostIP;
Process process = new Process();
log.Info("Starting " + command);
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command;
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += (s, e) => log.Info(e.Data);
process.ErrorDataReceived += (s, e) => log.Info(e.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();

This throws error :

'C:\Program' is not recognized as an internal or external command,...

I have looked at the similar issue posted Here and tried the /s option but no heed. What am i missing here ?

Community
  • 1
  • 1
Karthik
  • 929
  • 2
  • 12
  • 24
  • 1
    Why don't you put the command arguments in the [ProcessStartInfo.Arguments](https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments(v=vs.110).aspx) property? – stuartd Sep 30 '16 at 10:16
  • 1
    The command which is working on command line doesn't have anything like `/c` but in code your command starts with `/c` which is actually looks like a command line parameter. – RBT Sep 30 '16 at 10:21
  • Yes @RBT, Its a option of cmd.exe. /C Carries out the command specified by string and then terminates. – Karthik Sep 30 '16 at 10:23
  • Why the down Vote ? Wow, Did this piece of code work for you as expected? If so please share. – Karthik Sep 30 '16 at 10:49

3 Answers3

0

As the error says, it is trying to execute C:\Program which basically means something is wrong with the way you are escaping quotes. May be try to escape " using - \"

R Jain
  • 486
  • 3
  • 9
0

You're asking cmd.exe to run the command C:\Program Files (x86)\MyApp\solr-6.2.0\bin\solr. It will not run "C:\Program Files (x86)\MyApp\solr-6.2.0\bin\solr" because the quotes are consumed by the invocation of cmd.exe.

I do not know the proper way to fix this. You probably need to add escaped quotes somehow.

usr
  • 168,620
  • 35
  • 240
  • 369
0

Enclosing the command with "(quotes) after /c solves the issue.

Example :

"/c ""c:\prog files\xyz\solr" start -c -z "blah,blah,blah" -h IP "

Basically the command that is getting executed is

> cmd.exe /c "C:\Program Files (x86)\MyAPP\solr-6.2.0\bin\solr" start -f -c -z "10.195.42.93:2181,10.195.42.92:2181" -h 10.195.42.92

This command fails, and this has nothing to do with c# process api or .net.

Karthik
  • 929
  • 2
  • 12
  • 24