1

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.

Community
  • 1
  • 1
nicholas
  • 2,969
  • 20
  • 39

1 Answers1

2
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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Ah, wasn't aware file path strings could be passed to the Uri class constructor. Also, surprised to find that `Uri.Scheme` does recognize sftp as a protocol, even though it is not listed in the table on the [msdn documentation site](http://msdn.microsoft.com/en-us/library/system.uri.scheme.aspx). – nicholas Aug 13 '12 at 00:54
  • 2
    Read more carefully. "The Scheme property returns the scheme used to initialize the Uri instance. **This property does not indicate that the scheme used to initialize the Uri instance was recognized**. The following table shows examples of _some possible values_ returned by the Scheme property." (emphasis added) – SLaks Aug 13 '12 at 14:09
  • Also, try `new Uri("abc:def")` – SLaks Aug 13 '12 at 14:09
  • Ah, the devil is in the details. I think I am like many programmers out there who tend to skim documentation for tables, pictures, and sample code. Very good info. – nicholas Aug 13 '12 at 17:55