C#: URLEncode and URLDecode?
Often one needs to make a call to a webserver in .NET, and just as often there is Querystring data that needs to be sent along with this request. Thanksfully .NET has a great WebClient class in System.net.WebClient. But you can’t just add the querystring data into your URL like such:
Wrong: WebClient.DownloadFile("http://www.daveamenta.com/file.aspx?mydata=this is a string that I need to send to the server?");
The data needs to be encoded so that the server will not misinterpret the data. This is an example of using an encoded string:
Right: WebClient.DownloadFile("http://www.daveamenta.com/file.aspx?mydata=this+is+a+string+that+I+need+to+send+to+the+server%3F");
The second string is encoded properly, this way the webserver will properly parse that last ?. This was done using the URLEncode function, .NET has built-in classes for this–but they are a bit hidden. They’re kept in the System.Web namespace. This means that if you are working with .NET on the desktop side, as opposed to ASP.NET, you’ll need to Add a Reference to System.Web.dll. Once you do, you’ll see new classes in System.Web that you can use for many things.
Using URLEncode in C#:
WebClient.DownloadFile( "http://www.daveamenta.com/file.aspx?mydata=" + System.Web.HttpUtility.UrlEncode( "this is a string that I need to send to the server?" ) );
It is unlikely you will have much use for URLDecode in a C# application, but it is applied the same way.
What exactly is URLEncode?
URLEncode returns a string in which all non-alphanumeric characters other than – (hyphen) _ (underscore), and . (period) have been replaced with a percent (%) sign followed by two hex digits that correspond to the charactor code and spaces encoded as plus (+) signs. Spaces can alternatively be encoded as %20. 20 is (16*2)+(1*0)=32 in decimal, which is the ASCII code for space.
URLDecode will reverse this operation.
C#: Find location of My Documents, Application Data, Program Files and other Windows directories
I’ve seen the (perhaps VB-style) way of getting the MyDocuments folder, by using %HOMEDIR%, a batch-style variable. Some of these environment variables can be modified by navigating to Control Panel -> System -> Advanced (tab). In Vista, you’ll need to select Advanced System Settings at the System page in order to invoke the System Properties dialog.
These environment varibles work as pathes, but they are not really the accepted way of doing things. What you should do, is use System.Environment.GetFolderPath(). This method takes a value from the System.Environment.SpecialFolder enum. GetFolderPath will return a string for your location. This is supported on all versionf of the .NET framework.
Below is an excerpt from MSDN of the possible values for SpecialFolder:
| Name | Description | |
|---|---|---|
| ApplicationData | The directory that serves as a common repository for application-specific data for the current roaming user. | |
| CommonApplicationData | The directory that serves as a common repository for application-specific data that is used by all users. | |
| LocalApplicationData | The directory that serves as a common repository for application-specific data that is used by the current, non-roaming user. | |
| Cookies | The directory that serves as a common repository for Internet cookies. | |
| Desktop | The logical Desktop rather than the physical file system location. | |
| Favorites | The directory that serves as a common repository for the user’s favorite items. | |
| History | The directory that serves as a common repository for Internet history items. | |
| InternetCache | The directory that serves as a common repository for temporary Internet files. | |
| Programs | The directory that contains the user’s program groups. | |
| MyComputer | The “My Computer” folder. | |
| MyMusic | The “My Music” folder. | |
| MyPictures | The “My Pictures” folder. | |
| Recent | The directory that contains the user’s most recently used documents. | |
| SendTo | The directory that contains the Send To menu items. | |
| StartMenu | The directory that contains the Start menu items. | |
| Startup | The directory that corresponds to the user’s Startup program group. | |
| System | The System directory. | |
| Templates | The directory that serves as a common repository for document templates. | |
| DesktopDirectory | The directory used to physically store file objects on the desktop. | |
| Personal | The directory that serves as a common repository for documents. | |
| MyDocuments | The “My Documents” folder. | |
| ProgramFiles | The program files directory. | |
| CommonProgramFiles | The directory for components that are shared across applications. |


