Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding file paths

Tags:

.net

file-io

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())) + "]", " ");
    }
like image 538
Micah Avatar asked Jun 23 '09 11:06

Micah


People also ask

What does \\ mean in Filepath?

means it's the current directory. / is normally used in paths to show the structure of files and folders. \\ is referring to \ (used double because \ is a special character) which is same as / but used differently depending on the OS.

How do you create a path file?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document.


3 Answers

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

like image 87
Zanoni Avatar answered Nov 07 '22 06:11

Zanoni


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.

like image 34
Noldorin Avatar answered Nov 07 '22 04:11

Noldorin


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.

like image 40
Vivek Avatar answered Nov 07 '22 04:11

Vivek