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\""
... ?