Part of my .NET application needs to take an input string (representing a URI) and return only the filename (and extension if applicable).
Now Path.GetFileName does this perfectly fine for most paths, with the following exception: if the input string is an HTTP path that consists only of a domain name (e.g. example.com) and port number (e.g. 10210), GetFileName returns the port number.
In simple English: the input string is http://example.com:10210, which is a URI that links to an internet radio stream.
When Path.GetFileName is called with that value, the return value is 10210.
Is this intended behavior? I can’t recall any platform that separates path segments with colons. Shouldn’t the return value be String.Empty?
EDIT
It seems like the clean solution would be a mashup between Uri.LocalPath and System.Web.VirtualPathUtility.GetFileName, but that implies re-targetting your project from a client profile to the full framework.
I think I’ll just go with removing everything before (and including) the last forward-slash symbol. The separator never changes in my scenario anyway. Doing that gives me example.com:10210 as the return value, which sort of makes sense for what I’m doing.
Looking at the docs on the Path class, it doesn’t really appear that they intend you to use it with http or ftp or anything like that. All of the examples are around local file paths and network share paths.
Based on some other anecdotal info from around the web, such as this article about Path.Combine, I’m led to believe that you shouldn’t use Path for HTTP.