EnvironmentPermission.GetPathList(EnvironmentPermissionAccess) 方法

定义

使用指定的 EnvironmentPermissionAccess 获取所有环境变量。

public:
 System::String ^ GetPathList(System::Security::Permissions::EnvironmentPermissionAccess flag);
public string GetPathList (System.Security.Permissions.EnvironmentPermissionAccess flag);
member this.GetPathList : System.Security.Permissions.EnvironmentPermissionAccess -> string
Public Function GetPathList (flag As EnvironmentPermissionAccess) As String

参数

flag
EnvironmentPermissionAccess

其中一个 EnvironmentPermissionAccess 值,表示环境变量访问的单一类型。

返回

针对所选标识的环境变量(以分号分隔)列表。

例外

flag 不是 EnvironmentPermissionAccess 的一个有效值。

- 或 -

flagAllAccess,表示环境变量访问的多个类型,或 NoAccess,它不表示任何类型的环境变量的访问。

示例

下面的代码示例演示如何使用 GetPathList 方法。 此代码示例是为 EnvironmentPermission 类提供的一个更大示例的一部分。

private bool SetGetPathListDemo()
{
    try
    {
        Console.WriteLine("********************************************************\n");
        Console.WriteLine("Creating an EnvironmentPermission with AllAccess rights for 'TMP'");
        EnvironmentPermission envPerm1 = new EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, "TMP");
        Console.WriteLine("Adding 'TEMP' to the write access list, and 'windir' to the read access list.");
        envPerm1.AddPathList(EnvironmentPermissionAccess.Write, "TEMP");
        envPerm1.AddPathList(EnvironmentPermissionAccess.Read, "windir");
        Console.WriteLine("Read access list before SetPathList = "
            + envPerm1.GetPathList(EnvironmentPermissionAccess.Read));
        Console.WriteLine("Setting read access to 'TMP'");
        envPerm1.SetPathList(EnvironmentPermissionAccess.Read, "TMP");
        Console.WriteLine("Read access list after SetPathList = "
            + envPerm1.GetPathList(EnvironmentPermissionAccess.Read));
        Console.WriteLine("Write access list = " + envPerm1.GetPathList(EnvironmentPermissionAccess.Write));
        Console.WriteLine("Write access environment variables = "
            + envPerm1.GetPathList(EnvironmentPermissionAccess.AllAccess));
    }
    catch (ArgumentException e)
    {
        // EnvironmentPermissionAccess.AllAccess cannot be used as a parameter for GetPathList.
        Console.WriteLine("An ArgumentException occurred as a result of using AllAccess. "
            + " This property cannot be used as a parameter in GetPathList, because it represents "
            + "more than one type of environment variable : \n" + e);
    }

    return true;
}
Private Function SetGetPathListDemo() As Boolean 
    Try
        Console.WriteLine("********************************************************" + vbLf)
        Console.WriteLine("Creating an EnvironmentPermission with AllAccess rights for 'TMP'")
        Dim envPerm1 As New EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, "TMP")
        Console.WriteLine("Adding 'TEMP' to the write access list, and 'windir' to the read access list.")
        envPerm1.AddPathList(EnvironmentPermissionAccess.Write, "TEMP")
        envPerm1.AddPathList(EnvironmentPermissionAccess.Read, "windir")
        Console.WriteLine("Read access list before SetPathList = " + envPerm1.GetPathList(EnvironmentPermissionAccess.Read))
        Console.WriteLine("Setting read access to 'TMP'")
        envPerm1.SetPathList(EnvironmentPermissionAccess.Read, "TMP")
        Console.WriteLine("Read access list after SetPathList = " + envPerm1.GetPathList(EnvironmentPermissionAccess.Read))
        Console.WriteLine("Write access list = " + envPerm1.GetPathList(EnvironmentPermissionAccess.Write))
        Console.WriteLine("Write access environment variables = " + envPerm1.GetPathList(EnvironmentPermissionAccess.AllAccess))
    Catch e As ArgumentException
        ' EnvironmentPermissionAccess.AllAccess cannot be used as a parameter for GetPathList.
        Console.WriteLine("An ArgumentException occurred as a result of using AllAccess. " + _
        " This property cannot be used as a parameter in GetPathList, because it represents " + _
        "more than one type of environment variable : " + vbLf + e.Message)
    End Try
    
    Return True

End Function 'SetGetPathListDemo

注解

使用此方法可获取当前权限的状态。 若要获取 ReadWrite 访问状态,需要两次调用此方法。

注意

参数 flag 限制为 的值 EnvironmentPermissionAccess,这些值表示单种类型的环境变量访问。 这些值为 ReadWrite。 可接受的 flag 值不包括 NoAccessAllAccess,它们不表示单一类型的环境变量访问。

适用于