views:

6087

answers:

3

I have this method for grabbing the file name from a string URI. What can I do to make it more robust?

private string GetFileName(string hrefLink)
{
    string[] parts = hrefLink.Split('/');
    string fileName = "";

    if (parts.Length > 0)
        fileName = parts[parts.Length - 1];
    else
        fileName = hrefLink;

    return fileName;
}
A: 

Truncate at the ? for query strings.

Daniel A. White
+24  A: 

You can just make a System.Uri object, and use IsFile to verify it's a file, then Uri.LocalPath to extract the filename.

This is much safer, as it provides you a means to check the validity of the URI as well.


Edit in response to comment:

To get just the full filename, I'd use:

Uri uri = new Uri(hreflink);
string filename = Path.GetFileName(uri.LocalPath);

This does all of the error checking for you, and is platform-neutral. All of the special cases get handled for you quickly and easily.

Reed Copsey
I agree, you should really use the Uri class as it already does this stuff for you. +1
DoctaJonez
Yup, as simple as it may seem to roll-your-own the Uri class has lots of pre-rolled parsing/validating/encoding goodies in it for you.
STW
Right, but I just need the file name, not the complete file path. Aren't I still left to do that step on the Uri.LocalPath?
paulwhit
@paulwhit: In that case, you should use Path.GetFileName on the results of Uri.LocalPath. This is a completely safe, highly checked way of handling it. I will edit my answer to include this. See: http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx
Reed Copsey
Works great for absolute Urls, doesn't work for relative though. The Uri class is very limited when working with relative urls.
Tom
A: 
using System.IO;

private String GetFileName(String hrefLink)
{
    return Path.GetFileName(hrefLink.Replace("/", "\\"));
}

THis assumes, of course, that you've parsed out the file name.

EDIT #2:

using System.IO;

private String GetFileName(String hrefLink)
{
    return Path.GetFileName(Uri.UnescapeDataString(hrefLink).Replace("/", "\\"));
}

This should handle spaces and the like in the file name.

Mike Hofer
Colons are not acceptable in paths on all platforms, so this sort of hack might fail on, say, Mono.NET running on a *nix variant. Better to use System.Uri since it is specifically designed to do what the OP needs.
richardtallent
A valid point! I always forget about Mono. I thought about spaces and the like, but not the colons.
Mike Hofer