Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recognize a path inside a string

Tags:

string

c#

regex

Just that... I get a string which contains a path to a file plus some arguments. How can I recognize the path? I thought about the index of the '.' in the file... but I don't like it.
What about using regular expressions? Can anyone point me in the right direction?

Regards

Edit: Theses are valid entries...
somefile.msi /a
C:\MyFolder\SomeFile.exe -i -d

I don't care much about the arguments cause once I have the path I'll assume the rest are arguments

like image 402
sebagomez Avatar asked Feb 03 '26 23:02

sebagomez


1 Answers

You can use System.IO.Path, and it's static methods.

bool isPath = System.IO.Path.GetDirectoryName(@"C:\MyFolder\SomeFile.exe -i -d") != String.Empty;
if (isPath)
{
    Console.WriteLine("The string contains a path");
}

The static Path class has several other methods which are useful as well, like .GetFilename, .GetExtension and .GetPathRoot.

You can also probably use System.IO.Directory and System.IO.File for additional features.

like image 50
Frode Lillerud Avatar answered Feb 05 '26 12:02

Frode Lillerud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!