Recipe 12.12. Accessing Special User and Windows Directories


Problem

You would like to access some of the Windows-defined special directories, such as My Documents, but you're not sure where they are.

Solution

Fortunately, you don't have to know where they really are. You need to access only the members of the My.Computer. FileSystem. SpecialDirectories object.

Discussion

Microsoft Windows uses several " special" directories to store user and system files. The locations of these directories are generally consistent across workstations of a certain platform (such as Windows XP), but users and administrators can alter some of the paths, and some of the paths differ between operating system releases. (Windows Vista will make several location changes to these paths.)

The My.Computer.FileSystem.SpecialDirectories object includes these member properties. Directory components appearing in angle brackets, such as <user>, should be substituted by the relevant values, such as the username in the case of <user>. The properties listed in the SpecialDirectories object include:


AllUsersApplicationData

The shared application data-storage directory used by all authorized users who log in to the workstation. In Windows XP, this directory is typically found at C:\Documents and Settings\All Users\Application Data.


CurrentUserApplicationData

The data-storage directory assigned to a specific authorized user and to the currently running .NET application on the workstation. This directory is considered part of the active user's "roaming" profile. In Windows XP, this directory is typically found at C:\Documents and Settings\<user>\Application Data\<company>\<application>\<version>.


Desktop

The full path to the current authorized user's Desktop directory, which defines the items appearing on the Windows desktop. In Windows XP, this directory is typically found at C:\Documents and Settings\<user>\Desktop.


MyDocuments

The My Documents directory, used for general file storage by the current authorized user. In Windows XP, this directory is typically found at C:\Documents and Settings\<user>\My Documents.


MyMusic

The My Music directory, used to store standard and digital-rights-protected audio data files for the current authorized user. In Windows XP, this directory is typically found at C:\Documents and Settings\<user>\My Documents\My Music.


MyPictures

The My Pictures directory, used to store digital images and video content for the current authorized user. In Windows XP, this directory is typically found at C:\Documents and Settings\<user>\My Documents\My Pictures.


ProgramFiles

The default software product installation directory used by all authorized users on the workstation. In Windows XP, this directory is typically found at C:\Program Files.


Programs

The Programs directory for the current authorized user's Start menu. In Windows XP, this directory is typically found at C:\Documents and Settings\<user>\Start Menu\Programs.


Temp

The temporary directory used by the current authorized user to store short-lived caching and logging data files. In Windows XP, this directory is typically found at C:\Documents and Settings\<user>\Local Settings\Temp.

There are several special Windows directoriesdirectories defined both for the current user and for shared use among all usersthat do not have equivalent properties listed in the SpecialDirectories object. The System.Environment object provides access to some of these special directories not made available through the My.Computer.FileSystem.SpecialDirectories object. For instance, to access the System directory on the local workstation (defined on my workstation as C:\WINDOWS\System32), use the following property:

 System.Environment.SystemDirectory 

You can access other special directory locations with the System.Environment.GetFolderPath( ) method, passing it one of the System.Environment. SpecialFolder enumeration values:

 ' ----- Display the user's "Favorites" directory. MsgBox(System.Environment.GetFolderPath( _    Environment.SpecialFolder.Favorites)) 

The System.Environment.SpecialFolder enumeration includes the members listed below. We have listed the typical location for each member as found on a Windows XP Professional workstation. Directory components appearing in angle brackets, such as <user>, should be substituted by the relevant values, such as the username in the case of <user>.


SpecialFolder.ApplicationData

A directory containing roaming application data for the current user.

C:\Documents and Settings\<user>\Application Data

SpecialFolder.CommonApplicationData

A directory containing shared application data for all users on the local workstation.

C:\Documents and Settings\All Users\Application Data

SpecialFolder.CommonProgramFiles

A directory containing shared files used by multiple installed applications.

C:\Program Files\Common Files

SpecialFolder.Cookies

A directory containing Internet-based cookies for the current user.

C:\Documents and Settings\<user>\Cookies

SpecialFolder.Desktop

The logical location of the Desktop directory, which is often the same as the physical location, but not always.

C:\Documents and Settings\<user>\Desktop

SpecialFolder.DesktopDirectory

The physical location of the Desktop directory, which is often the same as the logical location, but not always.

C:\Documents and Settings\<user>\Desktop

SpecialFolder.Favorites

A directory containing shortcuts to the user's favorite Internet-based and local items.

C:\Documents and Settings\<user>\Favorites

SpecialFolder.History

A directory containing a set of web-site shortcuts for recently visited locations.

C:\Documents and Settings\<user>\Local Settings\History

SpecialFolder.InternetCache

A directory containing content recently accessed over the Internet.

C:\Documents and Settings\<user>\Local Settings\Temporary Internet Files

SpecialFolder.LocalApplicationData

A directory containing nonroaming application data for the current user.

C:\Documents and Settings\<user>\Local Settings\Application Data

SpecialFolder.MyComputer

The directory representing the "My Computer" feature on the Windows desktop. On most systems this returns a null or empty string because My Computer is an artificial view, not a true directory.


SpecialFolder.MyDocuments

The My Documents directory for the current user.

C:\Documents and Settings\<user>\My Documents

SpecialFolder.MyMusic

The audio media directory for the current user.

C:\Documents and Settings\<user>\My Documents\My Music

SpecialFolder.MyPictures

The image and video media directory for the current user.

C:\Documents and Settings\<user>\My Documents\My Pictures

SpecialFolder.Personal

The personal document directory for the current user. This is typically the My Documents directory.

C:\Documents and Settings\<user>\My Documents

SpecialFolder.ProgramFiles

The shared installation directory for applications on the local workstation.

C:\Program Files

SpecialFolder.Programs

The current user's "Programs" area within the Start menu.

C:\Documents and Settings\<user>\Start Menu\Programs

SpecialFolder.Recent

A directory of shortcuts to files recently used by the current user.

C:\Documents and Settings\<user>\Recent

SpecialFolder.SendTo

A directory of "Send To" target shortcuts for the current user.

C:\Documents and Settings\<user>\SendTo

SpecialFolder.StartMenu

The top-level Start menu directory for the current user.

C:\Documents and Settings\<user>\Start Menu

SpecialFolder.Startup

The current user's "Startup" area within the Start menu.

C:\Documents and Settings\<user>\Start Menu\Programs\Startup

SpecialFolder.System

The System directory that stores the primary Windows system components.

C:\WINDOWS\System32

SpecialFolder.Templates

A directory of new-file templates used when creating new files through Windows Explorer. This is not the same as the directory used to store Microsoft Word templates or other similar application-specific templates.

C:\Documents and Settings\<user>\Templates



Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net