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())) + "]", " ");
}