Our clients have a string stored in their registry which is an executable command. The string may or may not have quotes around the target path, and may or may not have parameters. For example:
"C:\path\file.exe" param1=value1 param2=value2
C:\path\file.exe param1=value1 param2=value2
"C:\path\file.exe"
C:\path\file.exe
"C:\path\file with spaces.exe" param1=value1 param2=value2
"C:\path\file with spaces.exe"
I need to get the directory of the target path, i.e. C:\path
.
- I tried
Path.GetDirectoryName
, but that fails with "Illegal characters in path." - I also tried creating a
ProcessStartInfo
object, but that doesn't seem to have the smarts to parse the string. - I fiddled with
Uri
, but didn't have any luck there either.
I have a solution using a regular expression (which I will post below as an answer), but this seems like a "there-must-be-a-built-in-.Net-helper-class-for-this" problem.
Is there a class I'm missing, or just not using the classes above correctly?