.NET TIP: Getting the Users Home, Temp or My Documents Directory by Charlie Calvert
Rating:
Ratings: 67
Rate IT
Abstract: IT WAS Not Immediately Obvious To Me How To Retrieve Commonly Used Directories by. Net. The Examples in this Article Outline How these Simple Tasks Are Performed.
By Charlie Calvert
A common need for programmers is to get the users home directory and other commonly used directories. In .NET, this involves using a combination of the Environment.SpecialFolders enumeration, and the Environment.GetEnvironmentVariable () function. The code in this article is written IN C #, But it will be easy to translate it Into pascal code.
Special Folders
Here is the environment.specialfolder enumeration:
Environment.SpecialFolder.ApplicationData Environment.SpecialFolder.System Environment.SpecialFolder.CommonApplicationData Environment.SpecialFolder.CommonProgramFiles Environment.SpecialFolder.Cookies Environment.SpecialFolder.Desktop Environment.SpecialFolder.DesktopDirectory Environment.SpecialFolder.Favorites Environment.SpecialFolder.History Environment.SpecialFolder.InternetCache Environment.SpecialFolder.LocalApplicationData Environment.SpecialFolder.MyComputer Environment.SpecialFolder.MyMusic Environment.SpecialFolder.MyPictures Environment.SpecialFolder.Personal Environment.SpecialFolder.ProgramFiles Environment.SpecialFolder.Programs Environment.SpecialFolder.Recent Environment.SpecialFolder.SendTo Environment.SpecialFolder.StartMenu
Here is how to use this enumeration:
String PersonalFolder = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
After Executing This Code, The Variable PersonalFolder Should Contain The Value of Your My Documents Directory.
Special Folders, Integer value of Enum, Dir on My SystemIn Listing 1 you see the values of all the special folders on my system. Note that this enumeration is not sequential. In particular, the integer value for the first member of the enumeration in. NET 1.1 IS 0, THE NEXT IS 2, THEN 5, THEN 6, ETC. I Got, THESE VALUES BY Writing this code: INT i = (int) sp. If someone sees a pattern here, let me know!
... .....
0 Desktop C: / Documents and Settings / Charlie / Desktop2 Programs C: / Documents and Settings / Charlie / Start Menu / Programs5 Personal D: / documents6 Favorites C: / Documents and Settings / Charlie / Favorites8 Recent C: / Documents and Settings / Charlie / Recent9 SendTo C: / Documents and Settings / Charlie / SendTo11 StartMenu C: / Documents and Settings / Charlie / Start Menu13 MyMusic D: / documents / My Music16 desktopDirectory C: / Documents and Settings / Charlie / Desktop17 MyComputer 26 ApplicationData C: / Documents and Settings / Charlie / Application Data28 LocalApplicationData C: / Documents and Settings / Charlie / Local Settings / Application Data32 InternetCache C: / Documents and Settings / Charlie / Local Settings / Temporary Internet Files33 Cookies C: / Documents and Settings / Charlie / Cookies34 History C: / Documents and Settings / Charlie / Local Settings / History35 CommonApplicationData C: / Documents and Settings / All Users / Application Data37 System C: / WINDOWS / System3238 ProgramFiles C: / Program Files39 MyPictures D : / Documents / My Pictures43 CommonProgramfiles C: / Program FilesCommon Files
Listing 2 shows the code for getting the values found in the previous listing. Note the use of the IList interface to access the list of strings stored in a ListBox. Becoming comfortable with interfaces is one of the core tasks for programmers migrating from Win32 to. Net. Java Programmers SHOULD ALREADY BE FAMILIAR WITH THIS PARADIGM.LISTING 2: The Code for Retrieving The VALUES Displayed IN LISTING 1.
Private static void showspecialfolder (Environment.SpecialFolder SP, IList List) {INT i = (int) sp; string s1 = string.format ("{1} {0} {2} {0} {0} {0} {3 } ", '/ t', i, sp.ToString (), Environment.GetFolderPath (sp)); list.Add (S1);} public static IList GetAllSpecialFolders (IList list) {ShowSpecialFolder (Environment.SpecialFolder.ApplicationData, list ); ShowSpecialFolder (Environment.SpecialFolder.System, list); ShowSpecialFolder (Environment.SpecialFolder.CommonApplicationData, list); ShowSpecialFolder (Environment.SpecialFolder.CommonProgramFiles, list); ShowSpecialFolder (Environment.SpecialFolder.Cookies, list); ShowSpecialFolder (Environment. SpecialFolder.Desktop, list); ShowSpecialFolder (Environment.SpecialFolder.DesktopDirectory, list); ShowSpecialFolder (Environment.SpecialFolder.Favorites, list); ShowSpecialFolder (Environment.SpecialFolder.History, list); ShowSpecialFolder (Environment.SpecialFolder.InternetCache, list) ShowSpecialFolder (Environment.SpecialFolder.local) ApplicationData, list); ShowSpecialFolder (Environment.SpecialFolder.MyComputer, list); ShowSpecialFolder (Environment.SpecialFolder.MyMusic, list); ShowSpecialFolder (Environment.SpecialFolder.MyPictures, list); ShowSpecialFolder (Environment.SpecialFolder.Personal, list); ShowSpecialFolder (Environment.SpecialFolder.ProgramFiles, list); ShowSpecialFolder (Environment.SpecialFolder.Programs, list); ShowSpecialFolder (Environment.SpecialFolder.Recent, list); ShowSpecialFolder (Environment.SpecialFolder.SendTo, list); ShowSpecialFolder (Environment.SpecialFolder.StartMenu , List); Return List;} private void button1_click_1 (Object sender, system.eventargs e) {getAllSpecialFolders (ListBox1.Items);
This code begins with the last method, button1_Click_1, which will be called when the user clicks on a button. The button click method calls the GetAllSpecialFolders () method. GetAllSpecialFolders () has one call to ShowSpecialFolders for each of the special folders that the C # API TRACKS for you. Each call to showspecialfolders creates One of the strings shown in listing 1: 0 Desktop C: / Documents and Settings / Charlie / Desktop
The showspecialfolders method begins by getting the integer value of the meser of the specialfolders Enumertion That Passed in As a parameter:
INT i = (int) sp;
This integer value appears at the beginning of the strings shown in Listing 1. For instance, it is the 0 before the word Desktop. This value is normally not important to developers, but I am showing it to you in case you are curious about the declaration of the SpecialFolders enumeration.In Microsoft's implementation of C #, we never see the source, so it is interesting to guess how it must be declared For instance, in this case, the enumeration might look something like this.:
ENUM SPECIALFOLDER {Desktop = 0, Programs = 2, Personal = 5, Favorites = 6, Recent = 8, ETC);
The next line of my ShowSpecialFolders method begins with a call to String.Format. The String.Format () method has a peculiar, but useful, syntax I have only seen in C #. Each of the instances of code that appears in curly braces is Replaced by one of the latter parameters passed to the method. for instance {0}, {1} and {2} get replaced with one of the parameters such as '/ t' or sp.tostring () Which is passed to string. . Format Note that '/ t;'.. is the second parameter It is the tab character, and will replace all instances of {0} The variable i is the third parameter, and the value stored in that variable will replace each instance of {1} .The code {2} is replaced by each instance of sp.ToString (). sp is a member of the SpecialFolder enumeration, and the ToString () method conveniently converts sp into a string representation of the enumeration member. That is , IT Converts SP Into A String Such AS "Desktop", "Programs", "Personal", etc. in The String Shown Above, The Value of i Appears AS 0 , and sp.to.ToString () APPEARS AS Desktop. Again, Programmers Don't Normal Need To make a call to find out the string value of an enumeration, but it is intending to know what you can do it if you so desire.
The last parameter, the one that goes into {3}, is the path, such as C:. / Documents and Settings / Charlie / Desktop I retrieved the path string by making the following call:. Environment.GetFolderPath (sp) Note that I Place Three Tabs in Front of the Path, To Separate It from the rest of the code:
String S1 = string.format ("{1} {0} {2} {0} {0} {0} {3}", 't', i,
Sp.toString (), Environment.GetFolderPath (SP));
I still had to manually edits some of the tabing to make it it coming 1.Useful Methods
Listing 3 shows some other useful methods. Note that I use the GetEnvironmentVariable call to retrieve the Home Directory and in the call to GetEnvTempDir (). Since environment variables are mutable, these calls are probably less reliable than other calls. Also, they may be OS dependant. In particular, I have only tested them on XP. Note, however, the GetTempDir () call, which uses what should be a more reliable method of retreiving the temporary directory. In particular, it uses the Path object. The GetMyDocumentsDir () Method is a wrapper around the environment.specialfolder enumeration.
Listing 3: Routines for getting the users Home Directory, Their My Documents Directory, And The Temp Directory.
public static String GetHomeDir () {return Environment.GetEnvironmentVariable ( "USERPROFILE");} public static String GetMyDocumentsDir () {return Environment.GetFolderPath (Environment.SpecialFolder.Personal);} public static String GetEnvTempDir () {return Environment.GetEnvironmentVariable ( "Temp");} public static string getTempdir () {return system.io.path.getTemppath ();
Summary
In this article you have learned a few simple tricks for getting system dependant information while using .NET. In particular, you learned about the Environment.SpecialFolder enumeration, and about the Environment.GetEnvironmentVariable method. The Environment class is part of the System namespace, .