Working With The File System & Streams (2)

Obtaining the paths of various Windows Standard folders –
Environment.GetFolderPath()
The method Environment.GetFolderPath() can be used to get the complete
paths of various windows standard folders on the current machine. The only
argument passed to the method is a value from the
System.Environment.SpecialFolder enumeration. Some of the more common
members of this enumeration are:

Member Description
ProgramFiles The program files folder where programs are usually installed
CommonProgramFiles The Common Files folder of Program Files.
DesktopDirectory The folder representing the desktop of user
Favorites The Favorites folder to store favorite links
History The History folder to store history files
Personal The My Documents folder
Programs The folder representing the Programs menu of Start Menu
Recent The Recent folder
SendTo The Send To folder
StartMenu The Start menu folder
Startup Folder of the Startup menu on the Start>>Programs menu
System The System folder of Windows folder
ApplicationData The Application Data folder
CommonApplicationData The Common Application Data folder
LocalApplicationData The Local Application Data folder
Cookies The folder used to store cookies setting


Let’s use these in a simple program to understand their functionality. We
can modify the previous program to include these results by changing the
btnGo_Click method to:


private void btnGo_Click(object sender, System.EventArgs e)
{
OperatingSystem os = Environment.OSVersion;
PlatformID OSid = os.Platform;
string[] drives = Environment.GetLogicalDrives();
string drivesString = "";
foreach(string drive in drives)
{
drivesString += drive + ", ";
}
drivesString = drivesString.TrimEnd(' ', ',');

lbx.Items.Add("Machine Name: \t" + Environment.MachineName);
lbx.Items.Add("Operating System: \t" + Environment.OSVersion);
lbx.Items.Add("Operating System ID:\t" + OSid);
lbx.Items.Add("Current Folder: \t" + Environment.CurrentDirectory);
lbx.Items.Add("CLR Version: \t" + Environment.Version);
lbx.Items.Add("Present Drives: \t" + drivesString);

lbx.Items.Add("Program Files: \t" +
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
lbx.Items.Add("Common Program Files:\t" +
Environment.GetFolderPath
(Environment.SpecialFolder.CommonProgramFiles));
lbx.Items.Add("Windows Desktop: \t" +
Environment.GetFolderPath
(Environment.SpecialFolder.DesktopDirectory));
lbx.Items.Add("Favorites: \t" +
Environment.GetFolderPath(Environment.SpecialFolder.Favorites));
lbx.Items.Add("History: \t" +
Environment.GetFolderPath(Environment.SpecialFolder.History));
lbx.Items.Add("Personal (My Documents:\t" +
Environment.GetFolderPath(Environment.SpecialFolder.Personal));
lbx.Items.Add("Start Menu's Program:\t" +
Environment.GetFolderPath(Environment.SpecialFolder.Programs));
lbx.Items.Add("Recent: \t" +
Environment.GetFolderPath(Environment.SpecialFolder.Recent));
lbx.Items.Add("Send To: \t" +
Environment.GetFolderPath(Environment.SpecialFolder.SendTo));
lbx.Items.Add("Start Menu: \t" +
Environment.GetFolderPath(Environment.SpecialFolder.StartMenu));
lbx.Items.Add("Startup: \t" +
Environment.GetFolderPath(Environment.SpecialFolder.Startup));
lbx.Items.Add("Windows System: \t" +
Environment.GetFolderPath(Environment.SpecialFolder.System));
lbx.Items.Add("Application Data: \t" +
Environment.GetFolderPath
(Environment.SpecialFolder.ApplicationData));
lbx.Items.Add("Common Application:\t" +
Environment.GetFolderPath
(Environment.SpecialFolder.CommonApplicationData));
lbx.Items.Add("Local Application Data:\t" +
Environment.GetFolderPath
(Environment.SpecialFolder.LocalApplicationData));
lbx.Items.Add("Cookies: \t" +
Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
}
When I executed the program with the above changes on my system, I got the
following result:
[PIC]
[SRC Download]

你可能感兴趣的:(windows,OS)