tags:

views:

25640

answers:

5

I have an application which I've developed for a friend. It sends a POST request to the VB forum software and logs someone in (with out setting cookies or anything).

Once the user is logged in I create a variable that creates a path on their local machine.

c:\tempfolder\date\username

The problem is that some usernames are throwing "Illegal chars" exception. For example if my username was mas|fenix it would throw an exception..

Path.Combine( _      
  Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData), _
  DateTime.Now.ToString("ddMMyyhhmm") + "-" + form1.username)

I don't want to remove it from the string, but a folder with their username is created through FTP on a server. And this leads to my second question. If I am creating a folder on the server can I leave the "illegal chars" in? I only ask this because the server is Linux based, and I am not sure if Linux accepts it or not..

EDIT: It seems that URL encode is NOT what I want.. Here's what I want to do:

old username = mas|fenix
new username = mas%xxfenix

Where %xx is the ASCII value or any other value that would easily identify the character.

+12  A: 

Url Encoding is easy in .NET. Use:

System.Web.HttpUtility.UrlEncode(string url)

If that'll be decoded to get the folder name, you'll still need to exclude characters that can't be used in folder names (*, ?, /, etc.)

teedyay
Does it encode every character thats not part of the alphabet?
masfenix
URL encoding converts characters that are not allowed in a URL into character-entity equivalents. List of unsafe characters: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
Ian Robinson
MSDN Link on HttpUtility.UrlEncode: http://msdn.microsoft.com/en-us/library/4fkewx0t.aspx
Ian Robinson
Please read my edit.
masfenix
It is good practice to put the full System.Web... part in your answer, it saves a lot of people a little time :) thanks
Liam
+7  A: 

UrlEncoding will do what you are suggesting here. With C#, you simply use HttpUtility, as mentioned.

You can also Regex the illegal characters and then replace, but this gets far more complex, as you will have to have some form of state machine (switch ... case, for example) to replace with the correct characters. Since UrlEncode does this up front, it is rather easy.

As for Linux versus windows, there are some characters that are acceptable in Linux that are not in Windows, but I would not worry about that, as the folder name can be returned by decoding the Url string, using UrlDecode, so you can round trip the changes.

Gregory A Beamer
A: 

Incorporate this to make file system safe folder names:

http://stackoverflow.com/questions/333175/is-there-a-way-of-making-strings-file-path-safe-in-c

rizzle
+21  A: 

You should encode only the user name or other part of the URL that could be invalid. URL encoding a URL can lead to problems since something like this:

string url = HttpUtility.UrlEncode("http://www.google.com/search?q=Example");

Will yield

http%3a%2f%2fwww.google.com%2fsearch%3fq%3dExample

This is obviously not going to work well. Instead, you should encode ONLY the value of the key/value pair in the query string, like this:

string url = "http://www.google.com/search?q=" + HttpUtility.UrlEncode("Example");

Hopefully that helps. Also, as teedyay mentioned, you'll still need to make sure illegal file-name characters are removed or else the file system won't like the path.

Dan Herbert
Using the HttpUtility.UrlPathEncode method should prevent the problem you're describing here.
DJ Pirtu
A: 

thanks Dan Herbert

i had same problem

HttpUtility.UrlPathEncode is the solution.

Kobaid