-1

I do not use C# often, however, I need to modify some code, which uses:

  Process new_process = new Process();
  new_process.StartInfo.FileName = "mytool.exe";

Now, I have to make a call, however with a rather biggish list of command line options, some of which contain spaces, and need to be quoted.

So, I've seen there exists this: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.argumentlist?view=net-7.0#system-diagnostics-processstartinfo-argumentlist

...
// or if you prefer collection property initializer syntax:

var info = new System.Diagnostics.ProcessStartInfo("cmd.exe")
{
    ArgumentList = {
        "/c",
        "dir",
        @"C:\Program Files\dotnet"
    }
};

// The corresponding assignment to the Arguments property is:

var info = new System.Diagnostics.ProcessStartInfo("cmd.exe")
{
    Arguments = "/c dir \"C:\\Program Files\\dotnet\""
};

I'd love to use ArgumentList - the problem is, when I try autocomplete in Visual Studio on new_process.StartInfo., I get only Arguments, no ArgumentList; and I think this application I'm trying to modify, targets .NET 4.6 - and the above page says for ArgumentList, that it applies to:

.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8

So, I guess I'm out of luck in terms of usage of ArgumentList here (not my app, so I really cannot make the decision to change the core version, if that is indeed the problem).

So if that is the case, is there some sort of a class in C# that would be essentially be a list/array container for strings, and which might have a "join_quoted" method or something, so that it joins the array elements with spaces, and quotes those elements that need to be quoted - so for

  • an input array {"/c", "dir", @"C:\Program Files\dotnet"}, I can automatically get
  • an output joined quoted string, which here would be "/c dir \"C:\\Program Files\\dotnet\""

... ?

sdbbs
  • 4,270
  • 5
  • 32
  • 87
  • 1
    Does this answer your question? [Escape command line arguments in c#](https://stackoverflow.com/questions/5510343/escape-command-line-arguments-in-c-sharp) – NineBerry Sep 01 '23 at 19:16
  • @NineBerry - not really; I'm aware of how complex it is to quote command line arguments from other languages, and the post you cited explains that on that low level (i.e. regex); here I'd like to avoid that all together (writing buggy regex myself), by using something that already exists as an API. And it already exists in the ArgumentList, except I cannot use it. So I'd like to know if there is such a ready-made C# method that could be applied to generic array of strings, and deliver a single joined string, readily quoted to be used for the command line? – sdbbs Sep 01 '23 at 19:24

0 Answers0