Working With The File System & Streams (2)

本文介绍如何使用Environment.GetFolderPath()方法来获取Windows系统中各种标准文件夹的完整路径,包括Program Files、Desktop、Favorites等,并通过一个示例程序展示了其具体用法。
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] 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值