tags:

views:

135

answers:

2

I have a program that receives something like this from a settings file:

"C:\Files\App 1\App.exe" "-param1:true -blah"

It receives this all as 1 string, but the Process object in C# needs the program and the arguments passed separately. Is there an easy way in C# to parse this, or a way to just pass the statement as it is without parsing it first?

+2  A: 

How about this?

var pattern = "\".*?\"";
var regex = new Regex(pattern);
var cmdString = "\"C:\\Files\\App 1\\App.exe\" \"-param1:true -blah\"";

var matches = regex.Matches(cmdString)
                   .OfType<Match>()
                   .Select(m => m.Value.Trim('\"'))
                   .ToArray();

var cmd = matches[0];
var arg = matches[1];

var proc = Process.Start(cmd, arg);
if (proc.Start())
    proc.WaitForExit();
Matthew Whited
A: 

Should be something like this.

Process.Start(new ProcessStartInfo() { FileName = @"C:\Files\App 1\App.exe", Arguments = "-param1:true -blah" });

If it all comes as one string you can Substring on the index of the first space character.

Travis Heseman
other than if a space is in the directory\file name
Matthew Whited
This won't work with paths that have a space in them.
Jon Tackabury
Yes, that completely slipped my mind.
Travis Heseman