FileIOPermission.GetPathList(FileIOPermissionAccess) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用指定的 FileIOPermissionAccess 权限获取所有文件和目录。
public:
cli::array <System::String ^> ^ GetPathList(System::Security::Permissions::FileIOPermissionAccess access);
public string[] GetPathList (System.Security.Permissions.FileIOPermissionAccess access);
member this.GetPathList : System.Security.Permissions.FileIOPermissionAccess -> string[]
Public Function GetPathList (access As FileIOPermissionAccess) As String()
参数
- access
- FileIOPermissionAccess
表示文件的单一类型访问的一个 FileIOPermissionAccess 的值。
返回
- String[]
一个数组,此数组包含了一组文件和文件夹的路径,所赋予的对这些文件和文件夹的访问权限由 access
参数指定。
例外
access
不是 FileIOPermissionAccess 的一个有效值。
示例
下面的代码示例演示方法的用法 GetPathList 。 此示例是为类提供的更大示例的一部分 FileIOPermission 。
bool SetGetPathListDemo()
{
try
{
Console::WriteLine( "********************************************************\n" );
FileIOPermission^ fileIOPerm1;
Console::WriteLine( "Creating a FileIOPermission with AllAccess rights for 'C:\\Examples\\Test\\TestFile.txt" );
fileIOPerm1 = gcnew FileIOPermission( FileIOPermissionAccess::AllAccess,"C:\\Examples\\Test\\TestFile.txt" );
Console::WriteLine( "Adding 'C:\\Temp' to the write access list, and \n 'C:\\Examples\\Test' to read access." );
fileIOPerm1->AddPathList( FileIOPermissionAccess::Write, "C:\\Temp" );
fileIOPerm1->AddPathList( FileIOPermissionAccess::Read, "C:\\Examples\\Test" );
array<String^>^paths = fileIOPerm1->GetPathList( FileIOPermissionAccess::Read );
Console::WriteLine( "Read access before SetPathList = " );
IEnumerator^ myEnum = paths->GetEnumerator();
while ( myEnum->MoveNext() )
{
String^ path = safe_cast<String^>(myEnum->Current);
Console::WriteLine( "\t{0}", path );
}
Console::WriteLine( "Setting the read access list to \n'C:\\Temp'" );
fileIOPerm1->SetPathList( FileIOPermissionAccess::Read, "C:\\Temp" );
paths = fileIOPerm1->GetPathList( FileIOPermissionAccess::Read );
Console::WriteLine( "Read access list after SetPathList = " );
IEnumerator^ myEnum1 = paths->GetEnumerator();
while ( myEnum1->MoveNext() )
{
String^ path = safe_cast<String^>(myEnum1->Current);
Console::WriteLine( "\t{0}", path );
}
paths = fileIOPerm1->GetPathList( FileIOPermissionAccess::Write );
Console::WriteLine( "Write access list after SetPathList = " );
IEnumerator^ myEnum2 = paths->GetEnumerator();
while ( myEnum2->MoveNext() )
{
String^ path = safe_cast<String^>(myEnum2->Current);
Console::WriteLine( "\t{0}", path );
}
Console::WriteLine( "Write access = \n{0}", fileIOPerm1->GetPathList( FileIOPermissionAccess::AllAccess ) );
}
catch ( ArgumentException^ e )
{
// FileIOPermissionAccess.AllAccess can not 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 file variable access. : \n{0}", e );
}
return true;
}
private bool SetGetPathListDemo()
{
try
{
Console.WriteLine("********************************************************\n");
FileIOPermission fileIOPerm1;
Console.WriteLine("Creating a FileIOPermission with AllAccess rights for 'C:\\Examples\\Test\\TestFile.txt");
fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess.AllAccess, "C:\\Examples\\Test\\TestFile.txt");
Console.WriteLine("Adding 'C:\\Temp' to the write access list, and \n 'C:\\Examples\\Test' to read access.");
fileIOPerm1.AddPathList(FileIOPermissionAccess.Write, "C:\\Temp");
fileIOPerm1.AddPathList(FileIOPermissionAccess.Read, "C:\\Examples\\Test");
string[] paths = fileIOPerm1.GetPathList(FileIOPermissionAccess.Read);
Console.WriteLine("Read access before SetPathList = ");
foreach (string path in paths)
{
Console.WriteLine("\t" + path);
}
Console.WriteLine("Setting the read access list to \n'C:\\Temp'");
fileIOPerm1.SetPathList(FileIOPermissionAccess.Read, "C:\\Temp");
paths = fileIOPerm1.GetPathList(FileIOPermissionAccess.Read);
Console.WriteLine("Read access list after SetPathList = ");
foreach (string path in paths)
{
Console.WriteLine("\t" + path);
}
paths = fileIOPerm1.GetPathList(FileIOPermissionAccess.Write);
Console.WriteLine("Write access list after SetPathList = ");
foreach (string path in paths)
{
Console.WriteLine("\t" + path);
}
Console.WriteLine("Write access = \n" +
fileIOPerm1.GetPathList(FileIOPermissionAccess.AllAccess));
}
catch (ArgumentException e)
{
// FileIOPermissionAccess.AllAccess can not 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 file variable access. : \n" + e);
}
return true;
}
Private Function SetGetPathListDemo() As Boolean
Try
Console.WriteLine("********************************************************" & ControlChars.Lf)
Dim fileIOPerm1 As FileIOPermission
Console.WriteLine("Creating a FileIOPermission with AllAccess rights for 'C:\Examples\Test\TestFile.txt")
fileIOPerm1 = New FileIOPermission(FileIOPermissionAccess.AllAccess, "C:\Examples\Test\TestFile.txt")
Console.WriteLine("Adding 'C:\Temp' to the write access list, and " & ControlChars.Lf & " 'C:\Examples\Test' to read access.")
fileIOPerm1.AddPathList(FileIOPermissionAccess.Write, "C:\Temp")
fileIOPerm1.AddPathList(FileIOPermissionAccess.Read, "C:\Examples\Test")
Dim paths As String() = fileIOPerm1.GetPathList(FileIOPermissionAccess.Read)
Console.WriteLine("Read access before SetPathList = ")
Dim path As String
For Each path In paths
Console.WriteLine((ControlChars.Tab & path))
Next path
Console.WriteLine("Setting the read access list to " & ControlChars.Lf & "'C:\Temp'")
fileIOPerm1.SetPathList(FileIOPermissionAccess.Read, "C:\Temp")
paths = fileIOPerm1.GetPathList(FileIOPermissionAccess.Read)
Console.WriteLine("Read access list after SetPathList = ")
For Each path In paths
Console.WriteLine((ControlChars.Tab & path))
Next path
paths = fileIOPerm1.GetPathList(FileIOPermissionAccess.Write)
Console.WriteLine("Write access list after SetPathList = ")
For Each path In paths
Console.WriteLine((ControlChars.Tab & path))
Next path
Dim pathList() As String
pathList = fileIOPerm1.GetPathList(FileIOPermissionAccess.AllAccess)
Catch e As ArgumentException
' FileIOPermissionAccess.AllAccess can not 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 file variable access. : " & ControlChars.Lf & e.ToString()))
End Try
Return True
End Function 'SetGetPathListDemo
注解
使用此方法可以获取当前权限的状态。 若要获取 Read
和访问的状态 Write
,需要对此方法进行两次调用。
备注
access
参数限制为的值 FileIOPermissionAccess ,这些值表示一种类型的文件访问。 这些值为 Read 、 Write 、 Append 和 PathDiscovery 。 可接受的值 access
不包括 NoAccess 和 AllAccess ,它们不表示单一类型的文件访问。