Directory Structure


The directory structure of Windows has changed in Windows Vista. There’s no longer a directory c:\Documents and Settings\<username>. It’s been replaced by the new folder c:\Users\<user- name>. Windows XP defines the subdirectory My Documents for storing user-specific data. Windows Vista defines c:\Users\<username>\Documents.

If you follow just the simple rule not to use hard-coded path values with the program, it doesn’t matter where the real folders are located. The folders differ with different Windows languages anyway. For special folders, use the Environment class and the SpecialFolder enumeration:

           string folder = Environment.GetFolderPath(Environment.SpecialFolder. Personal); 

Some of the folders defined by the SpecialFolder enumeration are described in the following table.

Open table as spreadsheet

Content

SpecialFolder Enumeration

Windows Vista default directory

User-specific documents

Personal

c:\Users\<User>\Documents

User-specific data for roaming users

ApplicationData

c:\Users\<User>\AppData\Roaming

User-specific data that is local to a system

LocalApplicationData

c:\Users\<User>\AppData\Local

Program files

ProgramFiles

c:\Program Files

Program files that are shared among different programs

CommonProgramFiles

c:\Program Files\Common Files

Application data common to all users

CommonApplicationData

c:\ProgramData

Tip 

At logoff, the content of roaming directories is copied to the server, so if the user logs on to a different system, the same content is copied and is thus available on all systems accessed by the user.

With the special folders, you must be careful that a normal user doesn’t have Write access to the program files directory. You can write user-specific data from the application to LocalApplicationData, or with roaming users to ApplicationData. Data that should be shared among different users can be written to CommonApplicationData.

Because many applications write content to the program files directory, they won’t run on Windows Vista without administrative privileges. Windows Vista has a solution for dealing with these programs - redirecting the folder to a virtual store that the applications can read from and write to without generating errors. This technique is called file virtualization.

Let’s verify this by writing a simple program that writes a file to the subdirectory WroxSampleApp in Program Files folder. Using Environment.GetFolderPath() with the SpecialFolder enumeration value ProgramFiles returns the Program Files folder; this folder is different depending on the Windows language used. The Program Files folder is combined with the directory WroxSampleApp, and in this directory the file samplefile.txt is written.

  string programFiles = Environment.GetFolderPath(       Environment.SpecialFolder.ProgramFiles); string appDir = Path.Combine(programFiles, "WroxSampleApp"); if (!Directory.Exists(appDir)) {    Directory.CreateDirectory(appDir); } string demoFile = Path.Combine(appDir, "samplefile.txt"); File.WriteAllText(demoFile, "test content"); 

When running the application without elevation, the file is not written to the directory c:\Program Files\WroxSampleApp. Instead, on an English Windows Vista system you can find the file in c:\Users\<username>\AppData\Local\Virtual Store\Program Files\WroxSampleApp.

As you can see, the data is stored in a user-specific directory and is not shared between different users on the same system. If this is a requirement, you have to start the application in elevated mode. When you run the application from an elevated Visual Studio process, the file is written to the Program Files folder instead of the virtual store, as an application started from an elevated process is elevated as well.

For reading files, a different mechanism is needed. Because an installation program is allowed to write content to the Program Files folder, it is valid for a program to read data from the Program Files folder. As soon as the program writes to this folder without being elevated, the redirection occurs. When it reads the written content again, the redirection is done with the read as well.

Virtualization is not only done with folders but also with registry entries. If the application writes to the registry key Software in the HKEY_LOCAL_MACHINE hive, it is redirected to the HKEY_CURRENT_USER hive. Instead of writing to HKLM_Software\{Manufacturer}, it writes to HKCU\Software\Classes\ VirtualStore\MACHINE\SOFTWARE\{Manufacturer}.

File and registry virtualization is only available for 32-bit applications. This feature is not available for 64-bit applications on Windows Vista.

Important 

Don’t use file and registry virtualization as a feature of your application. It is better to fix the application than to write to the Program Files folder and the HKLM registry hive without elevated user privileges. Redirection is only a temporary means to fix broken applications.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

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