. Net tip: getting the users home, temp or my clients directory by Charlie calvert

Source: Internet
Author: User

A common need for programmers is to get the users home directory and other commonly used directories. In. net, this involves using a combination ofEnvironment.SpecialfoldersEnumeration, andEnvironment.Getenvironmentvariable() Function. The code in this article is written in C #, But it shoshould be easy to translate it into Pascal code.

Special folders

Here isEnvironment.SpecialfolderEnumeration:

Environment. specialfolder. applicationdata
Environment. specialfolder. System
Environment. specialfolder. commonapplicationdata
Environment. specialfolder. commonprogramfiles
Environment. specialfolder. Cookies
Environment. specialfolder. Desktop
Environment. specialfolder. Revoke topdirectory
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 shoshould contain the value of your my documents directory.

Special folders, integer value of Enum, Dir on my system

In listing 1 you see the values of all the special folders on my system. note that this enumeration is not sequential. in particle, 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!

Listing 1: The values of the specialfolder enumeration on my system. The number in the first column is the integer value of the participating specialfolder member shown in the second column.

0 hosts topc: \ Documents ents and Settings \ Charlie \ Desktop
2 programsc: \ Documents and Settings \ Charlie \ Start Menu \ Programs
5 personald: \ Documents ents
6 favoritesc: \ Documents and Settings \ Charlie \ favorites
8 recentc: \ Documents ents and Settings \ Charlie \ recent
9 sendtoc: \ Documents ents and Settings \ Charlie \ sendto
11 startmenuc: \ Documents ents and Settings \ Charlie \ Start Menu
13 mymusicd: \ Documents \ my music
16 topics topdirectoryc: \ Documents ents and Settings \ Charlie \ Desktop
17 mycomputer
26 applicationdatac: \ Documents ents and Settings \ Charlie \ Application Data
28 localapplicationdatac: \ Documents and Settings \ Charlie \ Local Settings \ Application Data
32 internetcachec: \ Documents ents and Settings \ Charlie \ Local Settings \ Temporary Internet Files
33 cookiesc: \ Documents ents and Settings \ Charlie \ cookies
34 historyc: \ Documents ents and Settings \ Charlie \ Local Settings \ history
35 commonapplicationdatac: \ Documents ents and Settings \ All Users \ Application Data
37 EMC: \ windows \ system32
38 programfilesc: \ Program Files
39 mypicturesd: \ Documents ents \ my pictures
43 commonprogramfilesc: \ Program filescommon files

Listing 2 shows the code for getting the values found in the previous listing. Note the use ofIlistInterface 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. net. java programmers shoshould 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. Specify topdirectory, list );
Showspecialfolder (environment. specialfolder. favorites, list );
Showspecialfolder (environment. specialfolder. History, list );
Showspecialfolder (environment. specialfolder. internetcache, list );
Showspecialfolder (environment. specialfolder. localapplicationdata, 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,Button#click_1, Which will be called when the user clicks on a button. The button click method calltheGetallspecialfolders ()Method.Getallspecialfolders ()Has one callShowspecialfoldersFor each of the special folders that the C # API tracks for you. Each callShowspecialfoldersCreates one of the strings shown in Listing 1:

0 hosts topc: \ Documents ents and Settings \ Charlie \ Desktop

The showspecialfolders method begins by getting the integer value of the member ofSpecialfoldersEnumeration that is 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. tostring () appears as desktop . again, programmers don't normally need to make a call to find out the string value of an enumeration, but it is interesting to know that 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 edit some of the tabbing to make it come out as evenly as it appears in Listing 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 CILS are probably less reliable than other CILS. also, they may be OS dependant. in particle, I have only tested them on XP. note, however, the gettempdir () call, which uses what shocould be a more reliable method of retreiving the temporary directory. in particle, 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 ents 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 ();

In this article you have learned a few simple tricks for getting system dependant information while using. net. In particle, you learned aboutEnvironment.SpecialfolderEnumeration, and aboutEnvironment. getenvironmentvariableMethod.EnvironmentClass is part ofSystemNamespace, So you shoshould not have to add any specialUsingStatements to your code other than the defaultUsing SystemReference which shoshould appear at the top of all C # files.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.