nicholas
nicholas

Reputation: 3047

Efficiently Distinguish File Path from URI / URL

I have a very simple file transfer application (C# .NET) that allows users to move files from one file location to another on the local filesystem or to move files to an SFTP location.

The file operations are defined by a simple XML file which identifies the source and destination of each copy activity and valid strings can be either local file paths C:\SomePath\SomeFile.txt, or by an SFTP URL sftp://somesite.com/somepath/somefile.txt.

The actual operations performed by the program differ greatly depending on the source and destination type. (Akin to how Windows explorer can take a path or URL).

My question is, apart from general string parsing or reg-ex matching, is there an efficient or built-in means to classify a given string as being a file path type (Drive letter, colon, backslash) and a URL (protocol, colon, double-slash, etc.)?

This question is similar to Distinguish a filename from an URL, but I'm looking for a .NET solution.

Upvotes: 1

Views: 2331

Answers (1)

SLaks
SLaks

Reputation: 887275

if (new Uri(someString).IsFile)

If the string is not a valid path or URI, you'll get an exception.

You can also check the Uri.Scheme property, which will return file, http, etc.

Upvotes: 2

Related Questions