12

I have a task to write an object that can receive a different type of paths/urls, and return what type of path/url it is. For example the path can be

1. [drive]:\Temp 
2. \\Temp 
3. Temp (assuming that it relative Temp), 
4. /Temp 
5. ~/Temp 
6. file://[drive]:/Temp 
7. file://Temp 
8. [scheme]://something/Temp

...and so on.

How I can check in C# if it's physical path, relative url, or absolute url?

I think it's relatively easy to know if it's relative or absolute uri, but how to know if it's UNC path?

I tried to use Uri object and it's IsUnc property, but it not really helps me....for c:\temp it returns false, for "/temp", "temp/" and "temp" it throws an exception that format is incorrect. Does exists any built in object in .NET 3.5 that can help me with this, or what algorithm i can use to determine the type of path?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Alex Dn
  • 5,465
  • 7
  • 41
  • 79
  • Well, "/temp" is indeed not an URI at all. What is the exact format you want to check? If it's just paths and URIs, you can first differentiate between them, and apply different techniques for each. – Vlad Nov 01 '12 at 12:57
  • Why "/temp" is not valid URI? I think it can interpreted as relative. I need to check all of that formats and decide what type it is...is it UNC path or is it URI path...how I can differentiate between them? – Alex Dn Nov 01 '12 at 13:01
  • Well, http://tools.ietf.org/html/rfc3986. It doesn't start with a scheme, for example (http: or file: would do) – Vlad Nov 01 '12 at 13:04
  • You can try to (1) parse the string as URI with `new Uri(s)` or `Uri.TryCreate`; (2) if fails, try treating the string as path – Vlad Nov 01 '12 at 13:09
  • I tried it, but for C:\Temp, new Uri() not fails, but IsUnc property returns false – Alex Dn Nov 01 '12 at 13:10
  • Well, it must be a quirk in Uri class, as C:\Temp is really not an URI. However, it's [not an UNC path](http://msdn.microsoft.com/en-us/library/gg465305%28v=prot.20%29.aspx) as well, it's local. Look up [here](http://en.wikipedia.org/wiki/Path_%28computing%29#Uniform_Naming_Convention) for possible path formats. – Vlad Nov 01 '12 at 13:14
  • Hm, so possible I not explaining correctly what I'm need...i need to return one of the following: IsAbsolute / IsRelative / Or all other...other is for local files or local network pathes. But URI with file:// should be interpreted to local or local network. – Alex Dn Nov 01 '12 at 13:19
  • .. I may not be understanding you well, but I imagine that I would approach this problem by using regular expressions and, based upon matching, I could figure which grouping to assign it to / maybe THEN use URI encoding – John Bustos Nov 01 '12 at 13:56

1 Answers1

28

Try this:

var paths = new[]
{
   @"C:\Temp",
   @"\\Temp",
   "Temp",
   "/Temp",
   "~/Temp",
   "file://C:/Temp",
   "file://Temp",
   "http://something/Temp"
};

foreach (string p in paths)
{
   Uri uri;
   if (!Uri.TryCreate(p, UriKind.RelativeOrAbsolute, out uri))
   {
      Console.WriteLine("'{0}' is not a valid URI", p);
   }
   else if (!uri.IsAbsoluteUri)
   {
      Console.WriteLine("'{0}' is a relative URI", p);
   }
   else if (uri.IsFile)
   {
      if (uri.IsUnc)
      {
         Console.WriteLine("'{0}' is a UNC path", p);
      }
      else
      {
         Console.WriteLine("'{0}' is a file URI", p);
      }
   }
   else
   {
      Console.WriteLine("'{0}' is an absolute URI", p);
   }
}

Output:

'C:\Temp' is a file URI
'\\Temp' is a UNC path
'Temp' is a relative URI
'/Temp' is a relative URI
'~/Temp' is a relative URI
'file://C:/Temp' is a file URI
'file://Temp' is a UNC path
'http://something/Temp' is an absolute URI

Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
  • Thanks, but for values 'C:\Temp', '\\Temp', 'file://C:/Temp' and 'file://Temp' my object should return "PhysicalPath" and not URI. – Alex Dn Nov 01 '12 at 14:02
  • Then change the 'IsFile' branch to print "PhysicalPath" instead of "UNC path" and "file URI". – Richard Deeming Nov 01 '12 at 14:08
  • System.Uri treats protocol-relative as relative, but in most security contexts those should be treated like an absolute path. So `http://gmail.com` is absolute, but `//gmail.com` is relative, even though it's going to a different domain. – yzorg Aug 14 '14 at 22:45
  • @yzorg: I can't reproduce that - in both 3.5 and 4.5, `new Uri("//gmail.com")` treats the string as a UNC path. However, it's still not correct, and it looks like a bug to me. – Richard Deeming Aug 18 '14 at 11:35
  • @RichardDeeming I don't care about UNC paths, I'm working in http/web applications full time. Browsers don't handle `file://` from `http://` without special configuration, so `file://` and UNC are dead to me. I think UNC features of System.Uri aren't a bug, but an anachronism of .NET being written 10 years ago (or more). – yzorg Aug 18 '14 at 12:33