0

My objective is to issue the following to a command line from within a C# Windows Forms application:

load.exe --ip="192.168.1.88" C:\MyFiles\file1.txt C:\MyFiles\file2.txt

The following is my current attempt and I have observed that it is not performing the desired behavior nor am I getting any build or runtime errors:

string ipAddress = "192.168.1.88"
public static void LoadMyFiles(string ipAddress)
    {
        Process process1 = new System.Diagnostics.Process();
        process1.StartInfo.FileName = @"C:\Program Files (x86)\MyProgram\load.exe";
        process1.StartInfo.UseShellExecute = false;
        process1.StartInfo.Arguments = "--ip=\"ipAddress\" C:\\MyFiles\\file1.txt\ C:\\MyFiles\\file2.txt\";
        process1.StartInfo.RedirectStandardOutput = true;
        process1.Start();
    }

An alternative attempt I tried for StartInfo.Arguments using String.Concat also failed to execute properly.

processL1.StartInfo.Arguments = String.Concat("--ip=\"", ipAddress, "\"C:\\MyFiles\\file1.txt C:\\MyFiles\\file2.txt");

I have validated the desired behavior occurs if I manually issue the aforementioned line in a cmd window and thus believe this has been narrowed down to a syntax issue.

Answers with regard to any syntax issues I have made are appreciated.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
ngm_code
  • 141
  • 1
  • 9
  • 2
    Both your examples show syntax errors. Just dump the string to the console or immediate window, or inspect them with the string inspector and carefully check their results. The former will not even compile `(\ )` and the latter will become `--ip="ipaddress"C:\MyFiles...`, missing a space between the arguments. Read the duplicate, and if you can't figure it out, edit your question to reate a [mcve]. Also try to read the output of the process to see if it gives you any hints. – CodeCaster Oct 14 '15 at 10:39
  • Thanks for the tips and I also took at the suggested duplicate link. It was useful and informative but contained the same information I have seen in my prior research. I noticed the emphasis on the importance of using an escape character, namely \ before whitespace. With that said the examples they give contain many whitespaces between arguments without an \ preceding them. For instance, 'startInfo.Arguments = "-sk server -sky exchange -pe -n CN=localhost -ir LocalMachine -is Root -ic MyCA.cer -sr LocalMachine -ss My MyAdHocTestCert.cer"'. – ngm_code Oct 15 '15 at 06:22
  • 1
    No, the point is that you have a \ before whitespace in your first example. That will not compile. – CodeCaster Oct 15 '15 at 06:29
  • According to the literature @"stuff" should be interpreted as an absolute literal for the 'stuff' between the quotes. However, it does not seem to work on double quotes that one wants to be interpreted as literals. @""" does not seem to be interpreted as a literal " – ngm_code Oct 15 '15 at 07:00
  • Another issue is the inconsistencies in Visual Studio. In the Locals Window the values for string.Concat show \\ for each \ in the file path, while the text visualizer shows a single \ for each \ in the path. – ngm_code Oct 15 '15 at 07:06
  • CodeCaster, I found out why @""" will not work. I was expecting the middle quote to be captured by the two outer ones but that's not how the compiler interprets it. It is closing the first double quote with the second and thus the third double quote is active again. This leaves the question open as to how to make a literal double quote. – ngm_code Oct 15 '15 at 07:23
  • I'll note that \" is not working either. – ngm_code Oct 15 '15 at 07:25
  • "\"" appears to be working. – ngm_code Oct 15 '15 at 07:33
  • Try searching. See [MSDN: 2.4.4.5 String literals](https://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx), [Why does .NET add an additional slash to the already existent slashes in a path?](http://stackoverflow.com/questions/5465923/why-does-net-add-an-additional-slash-to-the-already-existent-slashes-in-a-path) – CodeCaster Oct 15 '15 at 07:42

0 Answers0