Micah
Micah

Reputation: 116050

Encoding file paths

Is there a built in method in .net for encoding file paths in the same way you would encode a url? For instance, if I have illegal characters in a file name, like "whatever:whatever" I would like it to encode the ":" so it's still there, just encoded so that the system will accept it. I'd like to do something like Path.Encode(fileName)

Anything like this out there?

Here's what I'm doing. I'm scraping wikipedia.org for a game I've created at www.wikipediamaze.com. When I do the screen scraping, I cache the results in a file in my app_data folder that matches the name of the current topic of the wikipedia site that I'm on. For instance, if I'm at the location:

http://www.wikipedia.org/wiki/Kevin_Bacon

Then I scrape that page, parse it, clean it, etc., and then cache on disk for faster retireval later. It get's stored at the location /App_Data/Kevin_Bacon (no file extension). This works great unless I'm on a page like

http://www.wikipedia.org/wiki/Wikipedia:About

trying to create a file at /App_Data/Wikipedia:About obviously doesn't work since the ':' character is illegal in a file name.

UPDATE

This works great for me:

    public static string EncodeAsFileName(this string fileName)
    {
        return Regex.Replace(fileName, "[" + Regex.Escape(                             
                new string(Path.GetInvalidFileNameChars())) + "]", " ");
    }

Upvotes: 12

Views: 9613

Answers (4)

Vivek
Vivek

Reputation: 16508

Do you need the filenames to be exactly as they are in the url?

If not, then use Base64 filenames. Whenever you create a file, convert the filename to base64 before writing or reading the file.

static public string EncodeTo64(string toEncode)
{
    byte[] bytes
          = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
    string stringBase64
          = System.Convert.ToBase64String(bytes);
    return stringBase64;
}

string fileName64 = Path.Combine(AppDataPath, EncodeTo64("Wikipedia:About"));

Edit: I realized that Base64 has '/' character which is an invalid character for filename. Replace the '/' with '-' after conversion.

Upvotes: 1

Zanoni
Zanoni

Reputation: 30958

Are invalid characters: \ / : ? "< > |

You just need to use GetInvalidFileNameChars function: (http://msdn.microsoft.com/library/system.io.path.getinvalidfilenamechars.aspx)

string sanitizedString = Regex.Replace("Wikipedia:About", "[" + Regex.Escape(new string(Path.GetInvalidFileNameChars())) + "]", "_");

All invalid characters will be replaced by _ (underscore).

Upvotes: 10

Lazarus
Lazarus

Reputation: 43064

Does this help? C# Sanitize File Name

Upvotes: 0

Noldorin
Noldorin

Reputation: 147240

No, I'm afraid there isn't anything that exists in the BCL.

You'll have to write your own function that replaces the invalid chars (by iterating over the input and using a StringBuilder, perhaps). The System.IO.Path.GetInvalidFileNameChars() method will however help you to do this.

The easiest solution however might just be to catch the exception that is thrown when an invalid char is specified within the path and provide feedback to the user.

Upvotes: 1

Related Questions