System.Environment.GetFolderPath 方法 (Environment.SpecialFolder)

方法描述

获取由指定枚举标识的系统特殊文件夹的路径。

语法定义(C# System.Environment.GetFolderPath 方法 (Environment.SpecialFolder) 的用法)

public static string GetFolderPath(
	Environment..::..SpecialFolder folder
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
folder System-Environment-SpecialFolder 标识系统特殊文件夹的枚举常数。
返回值 System.String 如果指定的系统特殊文件夹实际存在于您的计算机上,则为到该文件夹的路径;否则为空字符串 ("")。 如果操作系统未创建文件夹、已删除现有文件夹,或者文件夹是不对应物理路径的虚拟目录(例如“我的电脑”),则该文件夹不会实际存在。

提示和注释

此方法检索指向系统特殊文件夹(如 Program Files、Programs、System 或 Startup)的路径,可用于访问公共信息。 特殊文件夹在默认情况下由系统设置,或者由用户在安装 Windows 的某个版本时显式进行设置。

folder 参数指定要检索的特殊文件夹,且该参数必须是 Environment.SpecialFolder 枚举中的一个值;任何其他值都将引发异常。

有关特殊文件夹的更多信息,请参见 CSIDL 值主题。

System.Environment.GetFolderPath 方法 (Environment.SpecialFolder)例子

下面的示例演示如何使用 GetFolderPath 方法返回并显示与 folder 参数相关联的路径。

// Sample for the Environment.GetFolderPath method
using System;

class Sample 
{
    public static void Main() 
    {
    Console.WriteLine();
    Console.WriteLine("GetFolderPath: {0}", 
                 Environment.GetFolderPath(Environment.SpecialFolder.System));
    }
}
/*
This example produces the following results:

GetFolderPath: C:\WINNT\System32
*/

异常

异常 异常描述
ArgumentException folder 不是 System.Environment.SpecialFolder 的成员。
PlatformNotSupportedException 当前平台不受支持。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。