다음을 통해 공유


FileIOPermission 클래스

파일과 폴더에 액세스하는 기능을 제어합니다. 이 클래스는 상속될 수 없습니다.

네임스페이스: System.Security.Permissions
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class FileIOPermission
    Inherits CodeAccessPermission
    Implements IUnrestrictedPermission
‘사용 방법
Dim instance As FileIOPermission
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class FileIOPermission : CodeAccessPermission, IUnrestrictedPermission
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class FileIOPermission sealed : public CodeAccessPermission, IUnrestrictedPermission
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class FileIOPermission extends CodeAccessPermission implements IUnrestrictedPermission
SerializableAttribute 
ComVisibleAttribute(true) 
public final class FileIOPermission extends CodeAccessPermission implements IUnrestrictedPermission

설명

이 권한은 FileIOPermissionAccess에서 제공하는 다음 네 형식의 파일 IO 액세스를 구별합니다.

  • Read: 파일의 내용에 대한 읽기 액세스 또는 파일의 길이나 마지막으로 수정한 시간 같은 정보에 대한 액세스입니다.

  • Write: 파일의 내용에 대한 쓰기 액세스 또는 파일의 이름 같은 정보를 변경할 수 있는 액세스입니다. 파일을 삭제하거나 덮어쓸 수도 있습니다.

  • Append: 파일 끝에만 쓸 수 있고 파일을 읽을 수는 없는 액세스입니다.

  • PathDiscovery: 경로 자체의 정보에 대한 액세스입니다. 이 값은 사용자 이름처럼 경로에 포함된 중요한 정보 및 경로에 표시된 디렉터리 구조에 대한 정보를 보호합니다. 그러나 이 값이 경로로 표시된 파일이나 폴더에 대한 액세스 권한을 부여하지는 않습니다.

이러한 모든 권한은 독립적이므로 한 액세스 형식에 대한 권한에 다른 권한은 포함되지 않습니다. 예를 들어, Write 권한에 Read 또는 Append 권한은 포함되지 않습니다. 하나 이상의 권한이 필요하면 다음 코드 예제에서처럼 비트 OR를 사용하여 필요한 권한을 결합할 수 있습니다. 파일 사용 권한은 정규 절대 경로로 정의되며, 정규 파일 경로로 호출되어야 합니다.

FileIOPermission은 파일과 폴더에 대한 보호되는 작업을 나타냅니다. File 클래스는 파일과 폴더에 대한 보안 액세스를 제공합니다. 보안 액세스 검사는 파일에 대한 핸들을 만들 때 수행됩니다. 파일에 대한 핸들을 만들 때 이 보안 액세스 검사를 수행하면 보안 검사가 성능에 주는 영향을 최소화할 수 있습니다. 일단 파일을 열면 여러 번 읽고 쓰는 것이 가능합니다. 파일이 열린 후에는 검사가 더 이상 수행되지 않습니다. 개체가 트러스트되지 않은 호출자에게 전달되면 잘못 사용될 수 있습니다. 예를 들어, 사용 권한이 더 적은 코드가 액세스할 수 있는 공용 전역 정적 변수에 파일 핸들을 저장할 수 없습니다.

FileIOPermissionAccess는 파일이나 폴더에서 수행될 수 있는 동작을 지정합니다. 또한 비트 OR로 이러한 동작을 결합하여 복잡한 인스턴스를 만들 수 있습니다.

폴더에 대한 액세스 권한에는 해당 폴더에 있는 모든 파일에 대한 액세스 권한뿐만 아니라 해당 하위 폴더에 있는 모든 파일과 폴더에 대한 액세스 권한도 포함됩니다. 예를 들어, C:\folder1\에 대한 Read 액세스 권한에는 C:\folder1\file1.txt, C:\folder1\folder2\, C:\folder1\folder2\file2.txt 등에 대한 Read 액세스 권한이 포함됩니다.

경고

Unrestricted FileIOPermission에서는 파일 시스템 안의 모든 경로에 대한 권한을 부여합니다. 여기에는 지정된 단일 파일에 액세스하는 데 사용되는 다중 경로 이름도 포함됩니다. 파일에 대한 액세스를 Deny하려면 해당 파일에 대해 가능한 모든 경로를 Deny해야 합니다. 예를 들어 \\server\share가 네트워크 드라이브 X에 매핑된 경우 \\server\share\file에 대한 액세스를 Deny하려면 \\server\share\file, X:\file 및 해당 파일에 액세스하기 위해 사용할 수 있는 다른 모든 경로를 Deny해야 합니다. 경로가 여러 개인 경우 PermitOnlyDeny를 함께 사용하는 것이 좋습니다. 위의 예제에서 \\server\share를 PermitOnly한 다음 \\server\share\file을 Deny하여 대체 경로를 완전히 제거할 수 있습니다. 이 주제 및 Deny와 함께 PermitOnly를 사용하는 방법에 대한 자세한 내용은 Deny 메서드 사용에서 "Deny를 사용할 때의 정식화 문제"를 참조하십시오.

참고

\\server\share\bogusfolder\..\file 형식의 경로는 보안 시스템에 의해 정규 형식인 \\server\share\file로 변환되므로, 정규 경로인 \\server\share\file만 Deny하면 되며 동일한 경로를 지정하는 데 사용되는 이러한 구문 변경에 대해 신경 쓸 필요는 없습니다. 점(.) 연산자는 하나 또는 여러 개 나타나거나 후행 공백 문자와 조합되어 나타날 수 있습니다. 점(.) 연산자 뒤에 오는 공백은 무시됩니다. 한 개의 점은 무시되므로 예를 들어 주소 C:\temp\test\.\test.txt는 C:\temp\test\test.txt라는 주소가 됩니다. 여러 개의 점은 ".."로 처리되므로 바로 앞의 디렉터리가 무시됩니다. 예를 들어, 주소 C:\temp\test\.... \test.txt는 C:\temp\test.txt 경로가 됩니다.

참고

Deny는 Windows NTFS 파일 시스템과 함께 사용할 때 가장 효과적입니다. NTFS의 보안은 FAT32보다 더욱 강력합니다. NTFS에 대한 자세한 내용은 Windows 설명서를 참조하십시오.

예제

다음 예제에서는 FileIOPermission을 사용하는 코드를 보여 줍니다. 다음 두 코드 줄 다음에 오는 f 개체는 클라이언트 컴퓨터의 로컬 디스크에 있는 모든 파일을 읽을 수 있는 권한을 나타냅니다.

Dim f As New FileIOPermission(PermissionState.None)
f.AllLocalFiles = FileIOPermissionAccess.Read
FileIOPermission f = new FileIOPermission(PermissionState.None);
f.AllLocalFiles = FileIOPermissionAccess.Read;
FileIOPermission^ f = gcnew FileIOPermission( PermissionState::None );
f->AllLocalFiles = FileIOPermissionAccess::Read;
FileIOPermission f = new FileIOPermission(PermissionState.None);
f.set_AllLocalFiles(FileIOPermissionAccess.Read);

다음 두 코드 줄 다음에 오는 f2 개체는 C:\test_r을 읽을 수 있고 C:\example\out.txt에 쓸 수 있는 권한을 나타냅니다. ReadWrite는 이전에 설명한 대로 파일/폴더 사용 권한을 나타냅니다.

Dim f2 As New FileIOPermission(FileIOPermissionAccess.Read, "C:\test_r")
f2.AddPathList(FileIOPermissionAccess.Write Or _
   FileIOPermissionAccess.Read, "C:\example\out.txt")
FileIOPermission f2 = new FileIOPermission(FileIOPermissionAccess.Read, "C:\\test_r");
f2.AddPathList(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, "C:\\example\\out.txt");
FileIOPermission^ f2 = gcnew FileIOPermission( FileIOPermissionAccess::Read,"C:\\test_r" );
f2->AddPathList( (FileIOPermissionAccess) (FileIOPermissionAccess::Write | FileIOPermissionAccess::Read), "C:\\example\\out.txt" );
FileIOPermission f2 = 
    new FileIOPermission(FileIOPermissionAccess.Read, "C:\\test_r");
f2.AddPathList(FileIOPermissionAccess.Write | 
    FileIOPermissionAccess.Read, "C:\\example\\out.txt");

다음 예제에서는 다수의 FileIOPermission 멤버를 보여 줍니다.

Imports System
Imports System.Security
Imports System.Security.Permissions
Imports System.Collections
Imports Microsoft.VisualBasic

<Assembly: CLSCompliant(True)> 

Public Class FileIOPermissionDemo

    ' IsSubsetOf determines whether the current permission is a subset of the specified permission.
    ' This method compares various FileIOPermission paths with FileIOPermissionAccess set to AllAccess.
    Private Function IsSubsetOfDemo() As Boolean

        Dim returnValue As Boolean = True
        Dim fileIO1 As String = ""
        Dim fileIO2 As String = ""
        Dim fileIOPerm1, fileIOPerm2 As FileIOPermission

        Dim fileIOGen1 As New FileIOGenerator()
        Dim fileIOGen2 As New FileIOGenerator()

        fileIOGen1.ResetIndex()
        While fileIOGen1.CreateFilePath(fileIO1)
            If fileIO1 = "" Then
                fileIOPerm1 = New FileIOPermission(PermissionState.None)
            Else
                fileIOPerm1 = New FileIOPermission(FileIOPermissionAccess.AllAccess, fileIO1)
            End If

            Console.WriteLine("**********************************************************" & ControlChars.Lf)

            fileIOGen2.ResetIndex()

            While fileIOGen2.CreateFilePath(fileIO2)
                If fileIO2 = "" Then
                    fileIOPerm2 = New FileIOPermission(PermissionState.None)
                Else
                    fileIOPerm2 = New FileIOPermission(FileIOPermissionAccess.AllAccess, fileIO2)
                End If
                Dim firstPermission As String = IIf(fileIO1 = "" Or fileIO1 Is Nothing, "null", fileIO1)
                Dim secondPermission As String = IIf(fileIO2 = "" Or fileIO2 Is Nothing, "null", fileIO2)
                If fileIOPerm2 Is Nothing Then
                    GoTo ContinueWhile2
                End If
                Try
                    If fileIOPerm1.IsSubsetOf(fileIOPerm2) Then

                        Console.WriteLine((firstPermission & " is a subset of " & secondPermission & ControlChars.Lf))
                    Else
                        Console.WriteLine((firstPermission & " is not a subset of " & secondPermission & ControlChars.Lf))
                    End If

                Catch e As Exception
                    Console.WriteLine(IIf("An exception was thrown for subset :" & fileIO1 = "", "null", IIf(fileIO1 & ControlChars.Lf & fileIO2 = "", "null", fileIO2 & ControlChars.Lf & e.ToString())))
                End Try
ContinueWhile2:
            End While
ContinueWhile1:
        End While
        Return returnValue
    End Function 'IsSubsetOfDemo

    ' Union creates a new permission that is the union of the current permission and the specified permission.
    Private Function UnionDemo() As Boolean

        Dim returnValue As Boolean = True

        Dim fileIO1 As String = ""
        Dim fileIO2 As String = ""
        Dim fileIOPerm1, fileIOPerm2 As FileIOPermission
        Dim fileIOPerm3 As IPermission

        Dim fileIOGen1 As New FileIOGenerator()
        Dim fileIOGen2 As New FileIOGenerator()

        fileIOGen1.ResetIndex()
        While fileIOGen1.CreateFilePath(fileIO1)
            If fileIO1 = "" Then
                fileIOPerm1 = New FileIOPermission(PermissionState.None)
            Else
                fileIOPerm1 = New FileIOPermission(FileIOPermissionAccess.Read, fileIO1)
            End If
            Console.WriteLine("**********************************************************" & ControlChars.Lf)
            fileIOGen2.ResetIndex()

            While fileIOGen2.CreateFilePath(fileIO2)

                Try
                    If fileIO2 = "" Then
                        fileIOPerm2 = New FileIOPermission(PermissionState.None)
                    Else
                        fileIOPerm2 = New FileIOPermission(FileIOPermissionAccess.Read, fileIO2)
                    End If
                    Dim firstPermission As String = IIf(fileIO1 = "" Or fileIO1 Is Nothing, "null", fileIO1)
                    Dim secondPermission As String = IIf(fileIO2 = "" Or fileIO2 Is Nothing, "null", fileIO2)
                    fileIOPerm3 = CType(fileIOPerm1.Union(fileIOPerm2), FileIOPermission)
                    fileIOPerm3 = fileIOPerm1.Union(fileIOPerm2)

                    If fileIOPerm3 Is Nothing Then
                        Console.WriteLine(("The union of " & firstPermission & "  and " & secondPermission & " is null."))
                    Else
                        Console.WriteLine(("The union of " & firstPermission & "  and " & secondPermission & " = " & ControlChars.Lf & ControlChars.Tab & CType(fileIOPerm3, FileIOPermission).GetPathList(FileIOPermissionAccess.Read)(0)))
                    End If
                Catch e As Exception
                    Console.WriteLine(("An exception was thrown for union " & e.ToString()))
                    returnValue = False
                End Try
ContinueWhile2:
            End While
ContinueWhile1:
        End While


        Return returnValue
    End Function 'UnionDemo

    ' Intersect creates and returns a new permission that is the intersection of the current 
    ' permission and the permission specified.
    Private Function IntersectDemo() As Boolean

        Dim returnValue As Boolean = True

        Dim fileIO1 As String = ""
        Dim fileIO2 As String = ""
        Dim fileIOPerm1, fileIOPerm2, fileIOPerm3 As FileIOPermission

        Dim fileIOGen1 As New FileIOGenerator()
        Dim fileIOGen2 As New FileIOGenerator()

        fileIOGen1.ResetIndex()
        While fileIOGen1.CreateFilePath(fileIO1)
            If fileIO1 = "" Then
                fileIOPerm1 = New FileIOPermission(PermissionState.None)
            Else
                fileIOPerm1 = New FileIOPermission(FileIOPermissionAccess.Read, fileIO1)
            End If
            Console.WriteLine("**********************************************************" & ControlChars.Lf)
            fileIOGen2.ResetIndex()

            While fileIOGen2.CreateFilePath(fileIO2)
                If fileIO2 = "" Then
                    fileIOPerm2 = New FileIOPermission(PermissionState.None)
                Else
                    fileIOPerm2 = New FileIOPermission(FileIOPermissionAccess.Read, fileIO2)
                End If
                Dim firstPermission As String = IIf(fileIO1 = "" Or fileIO1 Is Nothing, "null", fileIO1)
                Dim secondPermission As String = IIf(fileIO2 = "" Or fileIO2 Is Nothing, "null", fileIO2)
                Try

                    fileIOPerm3 = CType(fileIOPerm1.Intersect(fileIOPerm2), FileIOPermission)
                    If Not (fileIOPerm3 Is Nothing) AndAlso Not (fileIOPerm3.GetPathList(FileIOPermissionAccess.Read) Is Nothing) Then

                        Console.WriteLine(("The intersection of " & firstPermission & "  and " & ControlChars.Lf & ControlChars.Tab & secondPermission & " = " & ControlChars.Lf & ControlChars.Tab & CType(fileIOPerm3, FileIOPermission).GetPathList(FileIOPermissionAccess.Read)(0)))
                    Else
                        Console.WriteLine(("The intersection of " & firstPermission & "  and " & secondPermission & " is null."))
                    End If
                Catch e As Exception
                    Console.WriteLine(("An exception was thrown for intersection " & e.ToString()))
                    returnValue = False
                End Try
ContinueWhile2:
            End While
ContinueWhile1:
        End While

        Return returnValue
    End Function 'IntersectDemo

    'Copy creates and returns an identical copy of the current permission.
    Private Function CopyDemo() As Boolean
        Dim returnValue As Boolean = True
        Dim fileIO1 As String = ""
        Dim fileIOPerm1, fileIOPerm2 As FileIOPermission
        Dim fileIOGen1 As New FileIOGenerator()
        Dim fileIOGen2 As New FileIOGenerator()

        fileIOGen1.ResetIndex()
        While fileIOGen1.CreateFilePath(fileIO1)
            If fileIO1 = "" Then
                fileIOPerm1 = New FileIOPermission(PermissionState.None)
            Else
                fileIOPerm1 = New FileIOPermission(FileIOPermissionAccess.Read, fileIO1)
            End If
            Console.WriteLine("**********************************************************" & ControlChars.Lf)
            fileIOGen2.ResetIndex()
            Try
                fileIOPerm2 = CType(fileIOPerm1.Copy(), FileIOPermission)
                If Not (fileIOPerm2 Is Nothing) Then
                    Console.WriteLine(("Result of copy = " & fileIOPerm2.ToString() & ControlChars.Lf))
                Else
                    Console.WriteLine("Result of copy is null. " & ControlChars.Lf)
                End If
            Catch e As Exception
                If (True.ToString()) Then
                    If fileIO1 = "" Then
                        Console.WriteLine("The target FileIOPermission is empty, copy failed.")

                    Else
                        Console.WriteLine(e.ToString())
                    End If
                End If
                GoTo ContinueWhile1
            End Try
ContinueWhile1:
        End While
        Return returnValue
    End Function 'CopyDemo

    ' ToXml creates an XML encoding of the permission and its current state; 
    ' FromXml reconstructs a permission with the specified state from the XML encoding. 
    Private Function ToFromXmlDemo() As Boolean

        Dim returnValue As Boolean = True

        Dim fileIO1 As String = ""
        Dim fileIOPerm1, fileIOPerm2 As FileIOPermission

        Dim fileIOGen1 As New FileIOGenerator()
        Dim fileIOGen2 As New FileIOGenerator()

        fileIOGen1.ResetIndex()
        While fileIOGen1.CreateFilePath(fileIO1)
            If fileIO1 = "" Then
                fileIOPerm1 = New FileIOPermission(PermissionState.None)
            Else
                fileIOPerm1 = New FileIOPermission(FileIOPermissionAccess.Read, fileIO1)
            End If
            Console.WriteLine("********************************************************" & ControlChars.Lf)
            fileIOGen2.ResetIndex()
            Try
                fileIOPerm2 = New FileIOPermission(PermissionState.None)
                fileIOPerm2.FromXml(fileIOPerm1.ToXml())
                Console.WriteLine(("Result of ToFromXml = " & fileIOPerm2.ToString() & ControlChars.Lf))

            Catch e As Exception
                Console.WriteLine(("ToFromXml failed :" & fileIOPerm1.ToString() & e.ToString()))
                GoTo ContinueWhile1
            End Try
ContinueWhile1:
        End While

        Return returnValue
    End Function 'ToFromXmlDemo

    ' AddPathList adds access for the specified files and directories to the existing state of the permission.
    ' SetPathList sets the specified access to the specified files and directories, replacing the existing state 
    ' of the permission.
    ' GetPathList gets all files and directories that have the specified FileIOPermissionAccess.
    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

    ' The AllFiles property gets or sets the permitted access to all files.
    ' The AllLocalFiles property gets or sets the permitted access to all local files.
    Private Function AllFilesDemo() As Boolean
        Try
            Console.WriteLine("********************************************************" & ControlChars.Lf)

            Dim fileIOPerm1 As FileIOPermission
            Console.WriteLine("Creating a FileIOPermission and adding read access for all files")
            fileIOPerm1 = New FileIOPermission(FileIOPermissionAccess.AllAccess, "C:\Examples\Test\TestFile.txt")
            fileIOPerm1.AllFiles = FileIOPermissionAccess.Read
            Console.WriteLine("AllFiles access = " & fileIOPerm1.AllFiles.ToString())
            Console.WriteLine("Adding AllAccess rights for local files.")
            fileIOPerm1.AllLocalFiles = FileIOPermissionAccess.AllAccess
            Console.WriteLine("AllLocalFiles access = " & fileIOPerm1.AllLocalFiles.ToString())

        Catch e As ArgumentException
            Console.WriteLine(e.ToString())
            Return False
        End Try

        Return True
    End Function 'AllFilesDemo

    ' Invoke all demos.
    Public Function RunDemo() As Boolean

        Dim ret As Boolean = True
        Dim retTmp As Boolean
        ' Call the IsSubsetOfPath demo.
        retTmp = IsSubsetOfDemo()
        If retTmp Then
            Console.Out.WriteLine("IsSubsetOf demo completed successfully.")
        Else
            Console.Out.WriteLine("IsSubsetOf demo failed.")
        End If
        ret = retTmp AndAlso ret

        ' Call the Union demo.
        retTmp = UnionDemo()
        If retTmp Then
            Console.Out.WriteLine("Union demo completed successfully.")
        Else
            Console.Out.WriteLine("Union demo failed.")
        End If
        ret = retTmp AndAlso ret

        ' Call the Intersect demo.  
        retTmp = IntersectDemo()
        If retTmp Then
            Console.Out.WriteLine("Intersect demo completed successfully.")
        Else
            Console.Out.WriteLine("Intersect demo failed.")
        End If
        ret = retTmp AndAlso ret


        ' Call the Copy demo.
        retTmp = CopyDemo()
        If retTmp Then
            Console.Out.WriteLine("Copy demo completed successfully.")
        Else
            Console.Out.WriteLine("Copy demo failed.")
        End If
        ret = retTmp AndAlso ret

        ' Call the ToFromXml demo.  
        retTmp = ToFromXmlDemo()
        If retTmp Then
            Console.Out.WriteLine("ToFromXml demo completed successfully.")
        Else
            Console.Out.WriteLine("ToFromXml demo failed.")
        End If
        ret = retTmp AndAlso ret

        ' Call the SetGetPathList demo. 
        retTmp = SetGetPathListDemo()
        If retTmp Then
            Console.Out.WriteLine("SetGetPathList demo completed successfully.")
        Else
            Console.Out.WriteLine("SetGetPathList demo failed.")
        End If
        ret = retTmp AndAlso ret

        ' Call the AllFiles demo.   
        retTmp = AllFilesDemo()
        If retTmp Then
            Console.Out.WriteLine("AllFiles demo completed successfully.")
        Else
            Console.Out.WriteLine("AllFiles demo failed.")
        End If
        ret = retTmp AndAlso ret

        Return ret
    End Function 'RunDemo

    ' Test harness.
    Public Overloads Shared Sub Main(ByVal args() As [String])
        Try
            Dim democase As New FileIOPermissionDemo()
            Dim ret As Boolean = democase.RunDemo()
            If ret Then
                Console.Out.WriteLine("FileIOPermission demo completed successfully.")
                Console.Out.WriteLine("Press the Enter key to exit.")
                Dim consoleInput As String = Console.ReadLine()
                System.Environment.ExitCode = 100
            Else
                Console.Out.WriteLine("FileIOPermission demo failed.")
                Console.Out.WriteLine("Press the Enter key to exit.")
                Dim consoleInput As String = Console.ReadLine()
                System.Environment.ExitCode = 101
            End If
        Catch e As Exception
            Console.Out.WriteLine("FileIOPermission demo failed")
            Console.WriteLine(e.ToString())
            Console.Out.WriteLine("Press the Enter key to exit.")
            Dim consoleInput As String = Console.ReadLine()
            System.Environment.ExitCode = 101
        End Try
    End Sub 'Main
End Class 'FileIOPermissionDemo


' This class generates FileIOPermission objects.

Friend Class FileIOGenerator


    Private myFile As String() = {"C:\Examples\Test\TestFile.txt", "C:\Examples\Test\", "C:\Examples\Test\..", "C:\Temp"}

    Private myAccess As FileIOPermissionAccess() = {FileIOPermissionAccess.AllAccess, FileIOPermissionAccess.Append, FileIOPermissionAccess.NoAccess, FileIOPermissionAccess.PathDiscovery, FileIOPermissionAccess.Read, FileIOPermissionAccess.Write}

    Private fileIndex As Integer = 0


    Public Sub New()

        ResetIndex()
    End Sub 'New


    Public Sub ResetIndex()
        fileIndex = 0
    End Sub 'ResetIndex

    ' Create a file path.
    Public Function CreateFilePath(ByRef file As String) As Boolean

        If fileIndex = myFile.Length Then

            file = ""
            fileIndex &= 1
            Return True
        End If
        If fileIndex > myFile.Length Then
            file = ""
            Return False
        End If

        file = myFile(fileIndex)
        fileIndex = fileIndex + 1

        Try
            Return True
        Catch e As Exception
            Console.WriteLine(("Cannot create FileIOPermission: " & file & " " & e.ToString()))
            file = ""
            Return True
        End Try
    End Function
End Class
using System;
using System.Security;
using System.Security.Permissions;
using System.Collections;

[assembly: CLSCompliant(true)]

public class FileIOPermissionDemo
{
    // IsSubsetOf determines whether the current permission is a subset of the specified permission.
    // This method compares various FileIOPermission paths with FileIOPermissionAccess set to AllAccess.
    private bool IsSubsetOfDemo()
    {

        bool returnValue = true;

        string fileIO1, fileIO2;
        FileIOPermission fileIOPerm1, fileIOPerm2;

        FileIOGenerator fileIOGen1 = new FileIOGenerator();
        FileIOGenerator fileIOGen2 = new FileIOGenerator();

        fileIOGen1.ResetIndex();
        while (fileIOGen1.CreateFilePath(out fileIO1 ))
        {
            if(fileIO1 == "")
                fileIOPerm1 = new FileIOPermission(PermissionState.None);
            else
            fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess.AllAccess, fileIO1);

            Console.WriteLine("**********************************************************\n");

            fileIOGen2.ResetIndex();

            while (fileIOGen2.CreateFilePath(out fileIO2))
            {
                if (fileIO2 == "")
                    fileIOPerm2 = new FileIOPermission(PermissionState.None);
                else
                    fileIOPerm2 = new FileIOPermission(FileIOPermissionAccess.AllAccess, fileIO2);
                string firstPermission = fileIO1 == "" | fileIO1 == null ? "null" : fileIO1;
                string secondPermission = fileIO2 == "" | fileIO2 == null ? "null" : fileIO2;
                if (fileIOPerm2 == null) continue;
                try
                {
                    if (fileIOPerm1.IsSubsetOf(fileIOPerm2))
                    {

                        Console.WriteLine(firstPermission + " is a subset of " + secondPermission + "\n");
                    }
                    else
                    {
                        Console.WriteLine(firstPermission + " is not a subset of " + secondPermission + "\n");
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine("An exception was thrown for subset :" + fileIO1 == "" ? "null" : fileIO1 + "\n" +
                        fileIO2 == "" ? "null" : fileIO2 + "\n" + e);
                }
            }
        }
        return returnValue;
    }

    // Union creates a new permission that is the union of the current permission and the specified permission.
    private bool UnionDemo()
    {

        bool returnValue = true;

        string fileIO1, fileIO2;
        FileIOPermission fileIOPerm1, fileIOPerm2;
        IPermission fileIOPerm3;

        FileIOGenerator fileIOGen1 = new FileIOGenerator();
        FileIOGenerator fileIOGen2 = new FileIOGenerator();

        fileIOGen1.ResetIndex();
        while (fileIOGen1.CreateFilePath( out fileIO1 ))
        {
            if (fileIO1 == "")
                fileIOPerm1 = new FileIOPermission(PermissionState.None);
            else
            fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess.Read, fileIO1);
            if (fileIO1 == null) continue;

            Console.WriteLine("**********************************************************\n");
            fileIOGen2.ResetIndex();

            while (fileIOGen2.CreateFilePath( out fileIO2 ))
            {
                if (fileIO2 == "")
                    fileIOPerm2 = new FileIOPermission(PermissionState.None);
                else
                    fileIOPerm2 = new FileIOPermission(FileIOPermissionAccess.Read, fileIO2);
                try
                {
                    string firstPermission = fileIO1 == "" | fileIO1 == null ? "null" : fileIO1;
                    string secondPermission = fileIO2 == "" | fileIO2 == null ? "null" : fileIO2;
                    fileIOPerm3 = (FileIOPermission)fileIOPerm1.Union(fileIOPerm2);
                    fileIOPerm3 = fileIOPerm1.Union(fileIOPerm2);

                    if (fileIOPerm3 == null)
                    {
                        Console.WriteLine("The union of " + firstPermission + " and " + secondPermission + " is null.");
                    }
                    else
                    {
                        Console.WriteLine("The union of " + firstPermission + " and " + secondPermission +
                            " = \n\t" + ((FileIOPermission)fileIOPerm3).GetPathList(FileIOPermissionAccess.Read)[0]);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("An exception was thrown for union " + e);
                    returnValue = false;
                }

            }

        }

        return returnValue;

    }

    // Intersect creates and returns a new permission that is the intersection of the current
    // permission and the permission specified.
    private bool IntersectDemo()
    {

        bool returnValue = true;

        string fileIO1, fileIO2;
        FileIOPermission fileIOPerm1, fileIOPerm2, fileIOPerm3;

        FileIOGenerator fileIOGen1 = new FileIOGenerator();
        FileIOGenerator fileIOGen2 = new FileIOGenerator();

        fileIOGen1.ResetIndex();
        while (fileIOGen1.CreateFilePath(out fileIO1 ))
        {
            if (fileIO1 == "")
                fileIOPerm1 = new FileIOPermission(PermissionState.None);
            else
                fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess.Read, fileIO1);

            Console.WriteLine("**********************************************************\n");
            fileIOGen2.ResetIndex();

            while (fileIOGen2.CreateFilePath( out fileIO2 ))
            {
                if (fileIO2 == "")
                    fileIOPerm2 = new FileIOPermission(PermissionState.None);
                else
                    fileIOPerm2 = new FileIOPermission(FileIOPermissionAccess.Read, fileIO2);
                string firstPermission = fileIO1 == "" | fileIO1 == null ? "null" : fileIO1;
                string secondPermission = fileIO2 == "" | fileIO2 == null ? "null" : fileIO2;
                try
                {

                    fileIOPerm3 = (FileIOPermission)fileIOPerm1.Intersect(fileIOPerm2);
                    if (fileIOPerm3 != null && fileIOPerm3.GetPathList(FileIOPermissionAccess.Read) != null)
                    {

                        Console.WriteLine("The intersection of " + firstPermission + " and \n\t" + secondPermission +
                            " = \n\t" + ((FileIOPermission)fileIOPerm3).GetPathList(FileIOPermissionAccess.Read)[0]);
                    }
                    else
                    {
                        Console.WriteLine("The intersection of " + firstPermission + " and " + secondPermission + " is null.");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("An exception was thrown for intersection " + e);
                    returnValue = false;
                }
            }
        }

        return returnValue;

    }

    //Copy creates and returns an identical copy of the current permission.
    private bool CopyDemo()
    {
        bool returnValue = true;
        string fileIO1;
        FileIOPermission fileIOPerm1, fileIOPerm2;
        FileIOGenerator fileIOGen1 = new FileIOGenerator();
        FileIOGenerator fileIOGen2 = new FileIOGenerator();

        fileIOGen1.ResetIndex();
        while (fileIOGen1.CreateFilePath( out fileIO1 ))
        {
            if (fileIO1 == "")
                fileIOPerm1 = new FileIOPermission(PermissionState.None);
            else
                fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess.Read, fileIO1);

            Console.WriteLine("**********************************************************\n");
            fileIOGen2.ResetIndex();
            try
            {
                fileIOPerm2 = (FileIOPermission)fileIOPerm1.Copy();
                if (fileIOPerm2 != null)
                {
                    Console.WriteLine("Result of copy = " + fileIOPerm2.ToString() + "\n");
                }
                else
                {
                    Console.WriteLine("Result of copy is null. \n");
                }
            }
            catch (Exception e)
            {
                {
                    if (fileIO1 == "")
                    {
                        Console.WriteLine("The target FileIOPermission is empty, copy failed.");

                    }
                    else
                        Console.WriteLine(e);
                }
                continue;
            }
        }
        return returnValue;
    }

    // ToXml creates an XML encoding of the permission and its current state;
    // FromXml reconstructs a permission with the specified state from the XML encoding.
    private bool ToFromXmlDemo()
    {

        bool returnValue = true;

        string fileIO1;
        FileIOPermission fileIOPerm1, fileIOPerm2;

        FileIOGenerator fileIOGen1 = new FileIOGenerator();
        FileIOGenerator fileIOGen2 = new FileIOGenerator();

        fileIOGen1.ResetIndex();
        while (fileIOGen1.CreateFilePath( out fileIO1 ))
        {
            if (fileIO1 == "")
                fileIOPerm1 = new FileIOPermission(PermissionState.None);
            else
                fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess.Read, fileIO1);

            Console.WriteLine("********************************************************\n");
            fileIOGen2.ResetIndex();
            try
            {
                fileIOPerm2 = new FileIOPermission(PermissionState.None);
                fileIOPerm2.FromXml(fileIOPerm1.ToXml());
                Console.WriteLine("Result of ToFromXml = " + fileIOPerm2.ToString() + "\n");

            }
            catch (Exception e)
            {
                Console.WriteLine("ToFromXml failed :" + fileIOPerm1.ToString() + e);
                continue;
            }
        }

        return returnValue;

    }

    // AddPathList adds access for the specified files and directories to the existing state of the permission.
    // SetPathList sets the specified access to the specified files and directories, replacing the existing state
    // of the permission.
    // GetPathList gets all files and directories that have the specified FileIOPermissionAccess.
    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;
    }

    // The AllFiles property gets or sets the permitted access to all files.
    // The AllLocalFiles property gets or sets the permitted access to all local files.
    private bool AllFilesDemo()
    {
        try
        {
            Console.WriteLine("********************************************************\n");

            FileIOPermission fileIOPerm1;
            Console.WriteLine("Creating a FileIOPermission and adding read access for all files");
            fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess.AllAccess, "C:\\Examples\\Test\\TestFile.txt");
            fileIOPerm1.AllFiles = FileIOPermissionAccess.Read;
            Console.WriteLine("AllFiles access = " + fileIOPerm1.AllFiles);
            Console.WriteLine("Adding AllAccess rights for local files.");
            fileIOPerm1.AllLocalFiles = FileIOPermissionAccess.AllAccess;
            Console.WriteLine("AllLocalFiles access = " + fileIOPerm1.AllLocalFiles);

        }
        catch (ArgumentException e)
        {
            Console.WriteLine(e);
            return false;
        }

        return true;
    }

    // Invoke all demos.
    public bool RunDemo()
    {

        bool ret = true;
        bool retTmp;
        // Call the IsSubsetOfPath demo.
        if (retTmp = IsSubsetOfDemo()) Console.Out.WriteLine("IsSubsetOf demo completed successfully.");
        else Console.Out.WriteLine("IsSubsetOf demo failed.");
        ret = retTmp && ret;

        // Call the Union demo.
        if (retTmp = UnionDemo()) Console.Out.WriteLine("Union demo completed successfully.");
        else Console.Out.WriteLine("Union demo failed.");
        ret = retTmp && ret;

        // Call the Intersect demo.
        if (retTmp = IntersectDemo()) Console.Out.WriteLine("Intersect demo completed successfully.");
        else Console.Out.WriteLine("Intersect demo failed.");
        ret = retTmp && ret;


        // Call the Copy demo.
        if (retTmp = CopyDemo()) Console.Out.WriteLine("Copy demo completed successfully.");
        else Console.Out.WriteLine("Copy demo failed.");
        ret = retTmp && ret;

        // Call the ToFromXml demo.
        if (retTmp = ToFromXmlDemo()) Console.Out.WriteLine("ToFromXml demo completed successfully.");
        else Console.Out.WriteLine("ToFromXml demo failed.");
        ret = retTmp && ret;

        // Call the SetGetPathList demo.
        if (retTmp = SetGetPathListDemo()) Console.Out.WriteLine("SetGetPathList demo completed successfully.");
        else Console.Out.WriteLine("SetGetPathList demo failed.");
        ret = retTmp && ret;

        // Call the AllFiles demo.
        if (retTmp = AllFilesDemo()) Console.Out.WriteLine("AllFiles demo completed successfully.");
        else Console.Out.WriteLine("AllFiles demo failed.");
        ret = retTmp && ret;

        return (ret);

    }
    // Test harness.
    public static void Main(String[] args)
    {
        try
        {
            FileIOPermissionDemo democase = new FileIOPermissionDemo();
            bool ret = democase.RunDemo();
            if (ret)
            {
                Console.Out.WriteLine("FileIOPermission demo completed successfully.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 100;
            }
            else
            {
                Console.Out.WriteLine("FileIOPermission demo failed.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 101;
            }
        }
        catch (Exception e)
        {
            Console.Out.WriteLine("FileIOPermission demo failed");
            Console.WriteLine(e.ToString());
            Console.Out.WriteLine("Press the Enter key to exit.");
            string consoleInput = Console.ReadLine();
            System.Environment.ExitCode = 101;
        }
    }
}


// This class generates FileIOPermission objects.

internal class FileIOGenerator
{


    private string[] myFile =
{
    "C:\\Examples\\Test\\TestFile.txt",
    "C:\\Examples\\Test\\",
    "C:\\Examples\\Test\\..",
    "C:\\Temp"
};

    private FileIOPermissionAccess[] myAccess =
{
    FileIOPermissionAccess.AllAccess,
    FileIOPermissionAccess.Append,
    FileIOPermissionAccess.NoAccess,
    FileIOPermissionAccess.PathDiscovery,
    FileIOPermissionAccess.Read,
    FileIOPermissionAccess.Write
};

    private int fileIndex = 0;

    public FileIOGenerator()
    {

        ResetIndex();
    }

    public void ResetIndex()
    {
        fileIndex = 0;
    }

    // Create a  file path string.
    public bool CreateFilePath( out string file )
    {

        if (fileIndex == myFile.Length)
        {

            file = "";
            fileIndex++;
            return true;

        }
        if (fileIndex > myFile.Length)
        {
            file = "";
            return false;
        }

        file = myFile[fileIndex++];

        try
        {
            return true;
        }
        catch (Exception e)
        {
            Console.WriteLine("Cannot create FileIOPermission: " + file + " " + e);
            file = "";
            return true;
        }
    }
}
using namespace System::Runtime::InteropServices;
using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Collections;

// This class generates FileIOPermission objects.

[assembly:CLSCompliant(true)];
private ref class FileIOGenerator
{
private:
   array<String^>^myFile;
   array<FileIOPermissionAccess>^myAccess;
   int fileIndex;

public:
   FileIOGenerator()
   {
      array<String^>^tempFile = {"C:\\Examples\\Test\\TestFile.txt","C:\\Examples\\Test\\","C:\\Examples\\Test\\..","C:\\Temp"};
      myFile = tempFile;
      array<FileIOPermissionAccess>^ tempAccess = {FileIOPermissionAccess::AllAccess,FileIOPermissionAccess::Append,FileIOPermissionAccess::NoAccess,FileIOPermissionAccess::PathDiscovery,FileIOPermissionAccess::Read,FileIOPermissionAccess::Write};
      myAccess = tempAccess;
      ResetIndex();
   }

   void ResetIndex()
   {
      fileIndex = 0;
   }


   // Create a file path.
   bool CreateFilePath(  [Out]interior_ptr<String^> file )
   {
      if ( fileIndex == myFile->Length )
      {
         
  
          *file = "";
         fileIndex++;
         return true;
      }

      if ( fileIndex > myFile->Length )
      {
          *file = "";
         return false;
      }

       *file = myFile[ fileIndex++ ];
      try
      {
         return true;
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Cannot create FileIOPermission: {0} {1}",  *file, e );
          *file = "";
         return true;
      }

   }

};

public ref class FileIOPermissionDemo
{
private:

   // IsSubsetOf determines whether the current permission is a subset of the specified permission.
   // This method compares various FileIOPermission paths with FileIOPermissionAccess set to AllAccess.
   bool IsSubsetOfDemo()
   {
      bool returnValue = true;
      String^ fileIO1;
      String^ fileIO2;
      FileIOPermission^ fileIOPerm1;
      FileIOPermission^ fileIOPerm2;
      FileIOGenerator^ fileIOGen1 = gcnew FileIOGenerator;
      FileIOGenerator^ fileIOGen2 = gcnew FileIOGenerator;
      fileIOGen1->ResetIndex();
      while ( fileIOGen1->CreateFilePath(  &fileIO1 ) )
      {
          if (fileIO1 == "")
              fileIOPerm1 = gcnew FileIOPermission(PermissionState::None);
          else
              fileIOPerm1 = gcnew FileIOPermission(FileIOPermissionAccess::AllAccess, fileIO1);

         Console::WriteLine( "**********************************************************\n" );
         fileIOGen2->ResetIndex();
         while ( fileIOGen2->CreateFilePath( &fileIO2 ) )
         {
            if (fileIO2 == "")
                fileIOPerm2 = gcnew FileIOPermission(PermissionState::None);
            else
              fileIOPerm2 = gcnew FileIOPermission(FileIOPermissionAccess::AllAccess, fileIO2);
            String^ firstPermission = fileIO1 == "" || fileIO1 == nullptr ? "null" : fileIO1;
            String^ secondPermission = fileIO2 == "" || fileIO2 == nullptr ? "null" : fileIO2;
            if ( fileIOPerm2 == nullptr )
                        continue;
            try
            {
               if ( fileIOPerm1->IsSubsetOf( fileIOPerm2 ) )
               {
                  Console::WriteLine( "{0} is a subset of {1}\n", firstPermission, secondPermission );
               }
               else
               {
                  Console::WriteLine( "{0} is not a subset of {1}\n", firstPermission, secondPermission );
               }
            }
            catch ( Exception^ e ) 
            {
               Console::WriteLine( "An exception was thrown for subset :{0}\n{1}\n{2}", (fileIO1->Equals( "" ) ? "null" : fileIO1), (fileIO2->Equals( "" ) ? "null" : fileIO2), e );
            }

         }
      }

      return returnValue;
   }


   // Union creates a new permission that is the union of the current permission and the specified permission.
   bool UnionDemo()
   {
      bool returnValue = true;
      String^ fileIO1;
      String^ fileIO2;
      FileIOPermission^ fileIOPerm1;
      FileIOPermission^ fileIOPerm2;
      IPermission^ fileIOPerm3;
      FileIOGenerator^ fileIOGen1 = gcnew FileIOGenerator;
      FileIOGenerator^ fileIOGen2 = gcnew FileIOGenerator;
      fileIOGen1->ResetIndex();
      while ( fileIOGen1->CreateFilePath(  &fileIO1 ) )
      {
         if (fileIO1 == "")
             fileIOPerm1 = gcnew FileIOPermission(PermissionState::None);
          else
              fileIOPerm1 = gcnew FileIOPermission(FileIOPermissionAccess::Read, fileIO1);

         Console::WriteLine( "**********************************************************\n" );
         fileIOGen2->ResetIndex();
         while ( fileIOGen2->CreateFilePath(  &fileIO2 ) )
         {
             if (fileIO2 == "")
                 fileIOPerm2 = gcnew FileIOPermission(PermissionState::None);
            else
              fileIOPerm2 = gcnew FileIOPermission(FileIOPermissionAccess::Read, fileIO2);
            try
            {
               if ( fileIOPerm2 == nullptr )
                              continue;
               String^ firstPermission = fileIO1 == "" || fileIO1 == nullptr ? "null" : fileIO1;
               String^ secondPermission = fileIO2 == "" || fileIO2 == nullptr ? "null" : fileIO2;
               fileIOPerm3 = dynamic_cast<FileIOPermission^>(fileIOPerm1->Union( fileIOPerm2 ));
               fileIOPerm3 = fileIOPerm1->Union( fileIOPerm2 );
               if ( fileIOPerm3 == nullptr )
               {
                  Console::WriteLine( "The union of {0}  and {1} is null.", firstPermission, secondPermission );
               }
               else
               {
                  Console::WriteLine( "The union of {0}  and {1} = \n\t{2}", firstPermission, secondPermission, (dynamic_cast<FileIOPermission^>(fileIOPerm3))->GetPathList( FileIOPermissionAccess::Read )[ 0 ] );
               }
            }
            catch ( Exception^ e ) 
            {
               Console::WriteLine( "An exception was thrown for union {0}", e );
               returnValue = false;
            }

         }
      }

      return returnValue;
   }


   // Intersect creates and returns a new permission that is the intersection of the current 
   // permission and the permission specified.
   bool IntersectDemo()
   {
      bool returnValue = true;
      String^ fileIO1;
      String^ fileIO2;
      FileIOPermission^ fileIOPerm1;
      FileIOPermission^ fileIOPerm2;
      FileIOPermission^ fileIOPerm3;
      FileIOGenerator^ fileIOGen1 = gcnew FileIOGenerator;
      FileIOGenerator^ fileIOGen2 = gcnew FileIOGenerator;
      fileIOGen1->ResetIndex();
      while ( fileIOGen1->CreateFilePath ( &fileIO1  ) )
      {
         if (fileIO1 == "")
             fileIOPerm1 = gcnew FileIOPermission(PermissionState::None);
          else
              fileIOPerm1 = gcnew FileIOPermission(FileIOPermissionAccess::Read, fileIO1);

         Console::WriteLine( "**********************************************************\n" );
         fileIOGen2->ResetIndex();
         while ( fileIOGen2->CreateFilePath( &fileIO2  ) )
         {
            if (fileIO2 == "")
                fileIOPerm2 = gcnew FileIOPermission(PermissionState::None);
            else
              fileIOPerm2 = gcnew FileIOPermission(FileIOPermissionAccess::Read, fileIO2);
            String^ firstPermission = fileIO1 == "" || fileIO1 == nullptr ? "null" : fileIO1;
            String^ secondPermission = fileIO2 == "" || fileIO2 == nullptr ? "null" : fileIO2;
            try
            {
               fileIOPerm3 = dynamic_cast<FileIOPermission^>(fileIOPerm1->Intersect( fileIOPerm2 ));
               if ( fileIOPerm3 != nullptr && fileIOPerm3->GetPathList( FileIOPermissionAccess::Read ) != nullptr )
               {
                  Console::WriteLine( "The intersection of {0}  and \n\t{1} = \n\t{2}", firstPermission, secondPermission, (dynamic_cast<FileIOPermission^>(fileIOPerm3))->GetPathList( FileIOPermissionAccess::Read )[ 0 ] );
               }
               else
               {
                  Console::WriteLine( "The intersection of {0}  and {1} is null.", firstPermission, secondPermission );
               }
            }
            catch ( Exception^ e ) 
            {
               Console::WriteLine( "An exception was thrown for intersection {0}", e );
               returnValue = false;
            }

         }
      }

      return returnValue;
   }


   //Copy creates and returns an identical copy of the current permission.
   bool CopyDemo()
   {
      bool returnValue = true;
      String^ fileIO1;
      FileIOPermission^ fileIOPerm1;
      FileIOPermission^ fileIOPerm2;
      FileIOGenerator^ fileIOGen1 = gcnew FileIOGenerator;
      FileIOGenerator^ fileIOGen2 = gcnew FileIOGenerator;
      fileIOGen1->ResetIndex();
      while ( fileIOGen1->CreateFilePath( &fileIO1 ) )
      {
         if (fileIO1 == "")
             fileIOPerm1 = gcnew FileIOPermission(PermissionState::None);
          else
              fileIOPerm1 = gcnew FileIOPermission(FileIOPermissionAccess::Read, fileIO1);

         Console::WriteLine( "**********************************************************\n" );
         fileIOGen2->ResetIndex();
         try
         {
            fileIOPerm2 = dynamic_cast<FileIOPermission^>(fileIOPerm1->Copy());
            if ( fileIOPerm2 != nullptr )
            {
               Console::WriteLine( "Result of copy = {0}\n", fileIOPerm2 );
            }
            else
            {
               Console::WriteLine( "Result of copy is null. \n" );
            }
         }
         catch ( Exception^ e ) 
         {
            {
               if ( fileIO1->Equals( "" ) )
               {
                  Console::WriteLine( "The target FileIOPermission is empty, copy failed." );
               }
               else
                              Console::WriteLine( e );
            }
            continue;
         }

      }

      return returnValue;
   }


   // ToXml creates an XML encoding of the permission and its current state; 
   // FromXml reconstructs a permission with the specified state from the XML encoding. 
   bool ToFromXmlDemo()
   {
      bool returnValue = true;
      String^ fileIO1;
      FileIOPermission^ fileIOPerm1;
      FileIOPermission^ fileIOPerm2;
      FileIOGenerator^ fileIOGen1 = gcnew FileIOGenerator;
      FileIOGenerator^ fileIOGen2 = gcnew FileIOGenerator;
      fileIOGen1->ResetIndex();
      while ( fileIOGen1->CreateFilePath( &fileIO1 ) )
      {
         if (fileIO1 == "")
             fileIOPerm1 = gcnew FileIOPermission(PermissionState::None);
          else
              fileIOPerm1 = gcnew FileIOPermission(FileIOPermissionAccess::Read, fileIO1);

         Console::WriteLine( "********************************************************\n" );
         fileIOGen2->ResetIndex();
         try
         {
            fileIOPerm2 = gcnew FileIOPermission( PermissionState::None );
            fileIOPerm2->FromXml( fileIOPerm1->ToXml() );
            Console::WriteLine( "Result of ToFromXml = {0}\n", fileIOPerm2 );
         }
         catch ( Exception^ e ) 
         {
            Console::WriteLine( "ToFromXml failed :{0}{1}", fileIOPerm1, e );
            continue;
         }

      }

      return returnValue;
   }


   // AddPathList adds access for the specified files and directories to the existing state of the permission.
   // SetPathList sets the specified access to the specified files and directories, replacing the existing state 
   // of the permission.
   // GetPathList gets all files and directories that have the specified FileIOPermissionAccess.
   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;
   }


   // The AllFiles property gets or sets the permitted access to all files.
   // The AllLocalFiles property gets or sets the permitted access to all local files.
   bool AllFilesDemo()
   {
      try
      {
         Console::WriteLine( "********************************************************\n" );
         FileIOPermission^ fileIOPerm1;
         Console::WriteLine( "Creating a FileIOPermission and adding read access for all files" );
         fileIOPerm1 = gcnew FileIOPermission( FileIOPermissionAccess::AllAccess,"C:\\Examples\\Test\\TestFile.txt" );
         fileIOPerm1->AllFiles = FileIOPermissionAccess::Read;
         Console::WriteLine( "AllFiles access = {0}", fileIOPerm1->AllFiles );
         Console::WriteLine( "Adding AllAccess rights for local files." );
         fileIOPerm1->AllLocalFiles = FileIOPermissionAccess::AllAccess;
         Console::WriteLine( "AllLocalFiles access = {0}", fileIOPerm1->AllLocalFiles );
      }
      catch ( ArgumentException^ e ) 
      {
         Console::WriteLine( e );
         return false;
      }

      return true;
   }


public:

   // Invoke all demos.
   bool RunDemo()
   {
      bool ret = true;
      bool retTmp;
      
      // Call the IsSubsetOfPath demo.
      if ( retTmp = IsSubsetOfDemo() )
            Console::WriteLine( "IsSubsetOf demo completed successfully." );
      else
            Console::WriteLine( "IsSubsetOf demo failed." );

      ret = retTmp && ret;
      
      // Call the Union demo.
      if ( retTmp = UnionDemo() )
            Console::WriteLine( "Union demo completed successfully." );
      else
            Console::WriteLine( "Union demo failed." );

      ret = retTmp && ret;
      
      // Call the Intersect demo. 
      if ( retTmp = IntersectDemo() )
            Console::WriteLine( "Intersect demo completed successfully." );
      else
            Console::WriteLine( "Intersect demo failed." );

      ret = retTmp && ret;
      
      // Call the Copy demo. 
      if ( retTmp = CopyDemo() )
            Console::WriteLine( "Copy demo completed successfully." );
      else
            Console::WriteLine( "Copy demo failed." );

      ret = retTmp && ret;
      
      // Call the ToFromXml demo. 
      if ( retTmp = ToFromXmlDemo() )
            Console::WriteLine( "ToFromXml demo completed successfully." );
      else
            Console::WriteLine( "ToFromXml demo failed." );

      ret = retTmp && ret;
      
      // Call the SetGetPathList demo. 
      if ( retTmp = SetGetPathListDemo() )
            Console::WriteLine( "SetGetPathList demo completed successfully." );
      else
            Console::WriteLine( "SetGetPathList demo failed." );

      ret = retTmp && ret;
      
      // Call the AllFiles demo. 
      if ( retTmp = AllFilesDemo() )
            Console::WriteLine( "AllFiles demo completed successfully." );
      else
            Console::WriteLine( "AllFiles demo failed." );

      ret = retTmp && ret;
      return (ret);
   }

};


// Test harness.
int main()
{
   try
   {
      FileIOPermissionDemo^ democase = gcnew FileIOPermissionDemo;
      bool ret = democase->RunDemo();
      if ( ret )
      {
         Console::WriteLine( "FileIOPermission demo completed successfully." );
         Console::WriteLine( "Press the Enter key to exit." );
         Console::ReadLine();
         System::Environment::ExitCode = 100;
      }
      else
      {
         Console::WriteLine( "FileIOPermission demo failed." );
         Console::WriteLine( "Press the Enter key to exit." );
         Console::ReadLine();
         System::Environment::ExitCode = 101;
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "FileIOPermission demo failed" );
      Console::WriteLine( e );
      Console::WriteLine( "Press the Enter key to exit." );
      Console::ReadLine();
      System::Environment::ExitCode = 101;
   }

}
// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, 
// ToXml, FromXml, GetPathList and SetPathList methods, and the 
// AllFiles and AllLocalFiles properties
// of the FileIOPermission class.

import System.*;
import System.Security.*;
import System.Security.Permissions.*;
import System.Collections.*;

/** @assembly CLSCompliant(true)
 */

public class FileIOPermissionDemo
{
    // IsSubsetOf determines whether the current permission is a 
    // subset of the specified permission.
    // This method compares various FileIOPermission paths with 
    // FileIOPermissionAccess set to AllAccess.
    private boolean IsSubsetOfDemo() 
    {
        boolean returnValue = true;
        String fileIO1[] = new String[1] ;
        String fileIO2[] = new String[1] ;
        FileIOPermission fileIOPerm1[] = new FileIOPermission[1] ;
        FileIOPermission fileIOPerm2[] = new FileIOPermission[1] ;
        
        FileIOGenerator fileIOGen1 =  new FileIOGenerator();
        FileIOGenerator fileIOGen2 =  new FileIOGenerator();
        
        fileIOGen1.ResetIndex();
        while(fileIOGen1.CreateFilePath(fileIOPerm1, fileIO1, 
            FileIOPermissionAccess.AllAccess)) {
            if (fileIOPerm1[0] == null) {
                continue ;
            }         
            Console.WriteLine("***********************" 
                + "***********************************\n");
            fileIOGen2.ResetIndex();
            
            while(fileIOGen2.CreateFilePath(fileIOPerm2, fileIO2, 
                FileIOPermissionAccess.AllAccess)) {
                String firstPermission = (fileIO1[0].equals("") == 
                    true | fileIO1[0] == null) ? "null" : fileIO1[0];
                String secondPermission = (fileIO2[0].equals("") == 
                    true | fileIO2[0] == null) ? "null" : fileIO2[0];
                if (fileIOPerm2[0] == null) {
                    continue ;
                }
                try {
                    if (fileIOPerm1[0].IsSubsetOf(fileIOPerm2[0])) {
                        Console.WriteLine((firstPermission 
                            + " is a subset of " 
                            + secondPermission + "\n"));
                    }
                    else {
                        Console.WriteLine((firstPermission 
                            + " is not a subset of " 
                            + secondPermission + "\n"));
                    }
                } 
                catch(System.Exception e) {
                    Console.WriteLine(("An exception was thrown for subset :" 
                        + ((fileIO1[0].equals("") == true) ? 
                        "null" : (fileIO1[0] + "\n" 
                        + ((fileIO2[0].equals("") == true) ? 
                        "null" : fileIO2[0]) + "\n" + e))));
                }
            }
        }
        return returnValue ;
    } //IsSubsetOfDemo   

    // Union creates a new permission that is the union of the current 
    // permission and the specified permission.
    private boolean UnionDemo() 
    {
        boolean returnValue = true;
        String fileIO1[] = new String[1];
        String fileIO2[] = new String[1];
        FileIOPermission fileIOPerm1[] = new FileIOPermission[1];
        FileIOPermission fileIOPerm2[] = new FileIOPermission[1];
        
        IPermission fileIOPerm3;
        
        FileIOGenerator fileIOGen1 =  new FileIOGenerator();
        FileIOGenerator fileIOGen2 =  new FileIOGenerator();
        
        fileIOGen1.ResetIndex();
        while(fileIOGen1.CreateFilePath(fileIOPerm1, 
            fileIO1, FileIOPermissionAccess.Read)) {
            if (fileIO1[0] == null) {
                continue ;
            }
            Console.WriteLine("*****************************" 
                + "*****************************\n");
            fileIOGen2.ResetIndex();
            
            while(fileIOGen2.CreateFilePath(fileIOPerm2, 
                fileIO2, FileIOPermissionAccess.Read)) {
                try {
                    if (fileIOPerm2[0] == null) {
                        continue ;
                    }
                    
                    String firstPermission = (fileIO1[0].equals("")==
                        true | fileIO1[0] == null) ? "null" : fileIO1[0];
                    String secondPermission = (fileIO2[0].equals("") == 
                        true | fileIO2[0] == null) ? "null" : fileIO2[0];
                    fileIOPerm3 =((FileIOPermission)(fileIOPerm1[0].
                        Union(fileIOPerm2[0])));
                    fileIOPerm3 = fileIOPerm1[0].Union(fileIOPerm2[0]);
                    
                    if (fileIOPerm3 == null) {
                        Console.WriteLine(("The union of " 
                            + firstPermission + " and " 
                            + secondPermission 
                            + " is null."));
                    }
                    else {
                        Console.WriteLine(("The union of " + firstPermission 
                            + " and " + secondPermission + " = \n\t" 
                            + ((FileIOPermission)(fileIOPerm3)).
                            GetPathList(FileIOPermissionAccess.Read).
                            get_Item( 0)));
                    }
                }
                catch(System.Exception e) {
                    Console.WriteLine(("An exception was " 
                        + "thrown for union " + e));
                    returnValue = false;
                }
            }
        }
        return returnValue ;
    } //UnionDemo    

    // Intersect creates and returns a new permission that is 
    // the intersection of the current
    // permission and the permission specified.
    private boolean IntersectDemo() 
    {
        boolean returnValue = true;
        String fileIO1[] = new String[1];
        String fileIO2[] = new String[1];
        FileIOPermission fileIOPerm1[] = new FileIOPermission[1];
        FileIOPermission fileIOPerm2[] = new FileIOPermission[1];
        FileIOPermission fileIOPerm3;
        
        FileIOGenerator fileIOGen1 =  new FileIOGenerator();
        FileIOGenerator fileIOGen2 =  new FileIOGenerator();
        
        fileIOGen1.ResetIndex();
        while(fileIOGen1.CreateFilePath(fileIOPerm1, fileIO1, 
            FileIOPermissionAccess.Read)) {
            if (fileIO1[0] == null) {
                continue ;
            }
            Console.WriteLine("***************************" 
                + "*******************************\n");
            fileIOGen2.ResetIndex();
            
            while(fileIOGen2.CreateFilePath(fileIOPerm2, fileIO2, 
                FileIOPermissionAccess.Read)) {
                if ( fileIOPerm2[0] == null  ) {
                    continue ;
                }
                String firstPermission = (fileIO1[0].equals("") == 
                    true | fileIO1[0] == null) ? "null" : fileIO1[0];
                String secondPermission = (fileIO2[0].equals("") == 
                    true | fileIO2[0] == null) ? "null" : fileIO2[0];
                try {
                    fileIOPerm3 =((FileIOPermission)(fileIOPerm1[0].
                    Intersect(fileIOPerm2[0])));
                    if (fileIOPerm3  != null && 
                        fileIOPerm3.GetPathList(FileIOPermissionAccess.Read)
                        != null) {
                        Console.WriteLine(("The intersection of " 
                            + firstPermission + " and \n\t" 
                            + secondPermission + " = \n\t" 
                            + ((FileIOPermission)(fileIOPerm3)).
                            GetPathList(FileIOPermissionAccess.Read).
                            get_Item( 0)));
                    }
                    else {
                        Console.WriteLine(("The intersection of " 
                            + firstPermission + " and " 
                            + secondPermission + " is null."));
                    }
                }
                catch(System.Exception e) {
                    Console.WriteLine(("An exception was " 
                        + "thrown for intersection " + e));
                        returnValue = false;
                }
            }
        }
        return returnValue ;
    } //IntersectDemo    

    //Copy creates and returns an identical copy of the current permission.
    private boolean CopyDemo() 
    {
        boolean returnValue = true;
        String fileIO1[] = new String[1];
        FileIOPermission fileIOPerm1[] = new FileIOPermission[1];
        FileIOPermission fileIOPerm2[] = new FileIOPermission[1];
        FileIOGenerator fileIOGen1 =  new FileIOGenerator();
        FileIOGenerator fileIOGen2 =  new FileIOGenerator();
        
        fileIOGen1.ResetIndex();
        while(fileIOGen1.CreateFilePath(fileIOPerm1, fileIO1, 
            FileIOPermissionAccess.Read)) {
                if (fileIO1 == null) {
                    continue ;
                }
                Console.WriteLine("**********************************" 
                + "************************\n");
                fileIOGen2.ResetIndex();
                try {
                    fileIOPerm2[0] =((FileIOPermission)(fileIOPerm1[0].Copy()));
                    if (fileIOPerm2[0]  != null) {
                        Console.WriteLine(("Result of copy = " 
                        + fileIOPerm2[0].ToString() + "\n"));
                    }
                    else {
                        Console.WriteLine("Result of copy is null. \n");
                    }
                }
                catch(System.Exception e) {
                    if (fileIO1[0].Equals("")) {
                        Console.WriteLine("The target FileIOPermission " 
                            + "is empty, copy failed.");
                    }
                    else {
                        Console.WriteLine(e);
                    }
                }
                continue ;
            }
        return returnValue ;
    } //CopyDemo   

    // ToXml creates an XML encoding of the permission and its current state;
    // FromXml reconstructs a permission with the specified state from the XML
    // encoding.
    private boolean ToFromXmlDemo() 
    {
        boolean returnValue = true;
        String fileIO1[] = new String[1];
        FileIOPermission fileIOPerm1[] = new FileIOPermission[1];
        FileIOPermission fileIOPerm2[] = new FileIOPermission[1];

        FileIOGenerator fileIOGen1 =  new FileIOGenerator();
        FileIOGenerator fileIOGen2 =  new FileIOGenerator();
        
        fileIOGen1.ResetIndex();
        while(fileIOGen1.CreateFilePath(fileIOPerm1, fileIO1, 
            FileIOPermissionAccess.Read)) {
            if (fileIOPerm1[0] == null) {
                continue ;
            }
            Console.WriteLine("**********************************" 
                + "**********************\n");
            fileIOGen2.ResetIndex();
            try {
                fileIOPerm2[0] = new FileIOPermission(PermissionState.None);
                fileIOPerm2[0].FromXml(fileIOPerm1[0].ToXml());
                Console.WriteLine(("Result of ToFromXml = " 
                    + fileIOPerm2[0].ToString() + "\n"));
            } 
            catch(System.Exception  e) {
                Console.WriteLine(("ToFromXml failed :" 
                    + fileIOPerm1[0].ToString() + e));
                continue ;
            }
        }
        return returnValue ;
    } //ToFromXmlDemo    

    // AllAccess
    // AddPathList adds access for the specified files and directories to the 
    // existing state of the permission.
    // SetPathList sets the specified access to the specified files and 
    // directories, replacing the existing state of the permission.
    // GetPathList gets all files and directories that have the specified 
    // FileIOPermissionAccess.
    private boolean 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 = ");
            for (int iCtr = 0; iCtr < paths.length; iCtr++) {
                String path = paths[iCtr];
                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 = ");
            for (int iCtr = 0; iCtr < paths.length; iCtr++) {
                String path = paths[iCtr];
                Console.WriteLine(("\t" + path));
            }            
            
            paths = fileIOPerm1.GetPathList(FileIOPermissionAccess.Write);
            Console.WriteLine("Write access list after SetPathList = ");
            for (int iCtr = 0; iCtr < paths.length; iCtr++) {
                String path = paths[iCtr];
                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 ;
    } //SetGetPathListDemo   

    // The AllFiles property gets or sets the permitted access to all files.
    // The AllLocalFiles property gets or sets the permitted access to 
    // all local files.
    private boolean AllFilesDemo() 
    {
        try {
            Console.WriteLine("***************************************" 
                + "*****************\n");
            
            FileIOPermission fileIOPerm1;
            Console.WriteLine("Creating a FileIOPermission and adding " 
                + "read access for all files");
            fileIOPerm1 = new FileIOPermission(
                FileIOPermissionAccess.AllAccess, 
                "C:\\Examples\\Test\\TestFile.txt");
            fileIOPerm1.set_AllFiles ( FileIOPermissionAccess.Read);
            Console.WriteLine(("AllFiles access = " 
                + fileIOPerm1.get_AllFiles()));
            Console.WriteLine("Adding AllAccess rights for local files.");
            fileIOPerm1.set_AllLocalFiles (FileIOPermissionAccess.AllAccess);
            Console.WriteLine(("AllLocalFiles access = " 
                + fileIOPerm1.get_AllLocalFiles()));
        } 
        catch(ArgumentException e) {
            Console.WriteLine(e);
            return false ;
        }
        return true ;
    } //AllFilesDemo

    // Invoke all demos.
    public boolean RunDemo() 
    {
        boolean ret = true;
        boolean retTmp;
        // Call the IsSubsetOfPath demo.
        if (retTmp = IsSubsetOfDemo()) {
            Console.get_Out().WriteLine("IsSubsetOf demo completed " 
                + "successfully.");
        }
        else {
            Console.get_Out().WriteLine("IsSubsetOf demo failed.");
        }
        ret = retTmp && ret;
        
        // Call the Union demo.
        if (retTmp = UnionDemo()) {
            Console.get_Out().WriteLine("Union demo completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("Union demo failed.");
        }
        ret = retTmp && ret;
        // Call the Intersect demo.
        if (retTmp = IntersectDemo()) {
            Console.get_Out().WriteLine("Intersect demo completed " 
                + "successfully.");
        }
        else {
            Console.get_Out().WriteLine("Intersect demo failed.");
        }
        ret = retTmp && ret;
        
        // Call the Copy demo.
        if (retTmp = CopyDemo()) {
            Console.get_Out().WriteLine("Copy demo completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("Copy demo failed.");
        }
        ret = retTmp && ret;
        
        // Call the ToFromXml demo.
        if (retTmp = ToFromXmlDemo()) {
            Console.get_Out().WriteLine("ToFromXml demo completed " 
                + "successfully.");
        }
        else {
            Console.get_Out().WriteLine("ToFromXml demo failed.");
        }
        ret = retTmp && ret;
        
        // Call the SetGetPathList demo.
        if (retTmp = SetGetPathListDemo()) {
            Console.get_Out().WriteLine("SetGetPathList demo completed " 
                + "successfully.");
        }
        else {
            Console.get_Out().WriteLine("SetGetPathList demo failed.");
        }
        ret = retTmp && ret;
        
        // Call the AllFiles demo.
        if (retTmp = AllFilesDemo()) {
            Console.get_Out().WriteLine("AllFiles demo completed " 
                + "successfully.");
        }
        else {
            Console.get_Out().WriteLine("AllFiles demo failed.");
        }
        ret = retTmp && ret;      
        return ret ;
    } //RunDemo

    // Test harness.
    public static void main(String[] args)
    {
        try {
            FileIOPermissionDemo democase =  new FileIOPermissionDemo();
            boolean ret = democase.RunDemo();
            if (ret) {
                Console.get_Out().WriteLine("FileIOPermission demo " 
                    + "completed successfully.");
                Console.get_Out().WriteLine("Press the Enter key to exit.");
                String consoleInput = Console.ReadLine();
                System.Environment.set_ExitCode(100);
            }
            else {
                Console.get_Out().WriteLine("FileIOPermission demo failed.");
                Console.get_Out().WriteLine("Press the Enter key to exit.");
                String consoleInput = Console.ReadLine();
                System.Environment.set_ExitCode(101);
            }
        }
        catch(System.Exception e) {
            Console.get_Out().WriteLine("FileIOPermission demo failed");
            Console.WriteLine(e.ToString());
            Console.get_Out().WriteLine("Press the Enter key to exit.");
            String consoleInput = Console.ReadLine();
            System.Environment.set_ExitCode(101);
        }
    } //main
} //FileIOPermissionDemo

// This class generates FileIOPermission objects.
class FileIOGenerator
{
    private String myFile[] =  {"C:\\Examples\\Test\\TestFile.txt", 
        "C:\\Examples\\Test\\", "C:\\Examples\\Test\\..", "C:\\Temp"};   
    private FileIOPermissionAccess myAccess[] = {
        FileIOPermissionAccess.AllAccess, 
        FileIOPermissionAccess.Append, 
        FileIOPermissionAccess.NoAccess, 
        FileIOPermissionAccess.PathDiscovery, 
        FileIOPermissionAccess.Read, 
        FileIOPermissionAccess.Write};   
    
    private int fileIndex = 0;

    public FileIOGenerator() 
    {
        ResetIndex();
    } //FileIOGenerator

    public void ResetIndex() 
    {
        fileIndex = 0;
    } //ResetIndex   

    // Create a FileIOPermission using FileIOPermissionAccess 
    // that is passed in.
    public boolean CreateFilePath(FileIOPermission fileIOPerm[], String file[], 
        FileIOPermissionAccess fpa) 
    {
        if (fileIndex == myFile.length) {
            fileIOPerm[0] = new FileIOPermission(PermissionState.None);

            file[0] = "";
            fileIndex++;
            return true ;
        }
        if (fileIndex > myFile.length) {
            fileIOPerm[0] = null;
            file[0] = "";
            return false ;
        }
        file[0] = myFile[fileIndex++];
        
        try {
            fileIOPerm[0] = new FileIOPermission(fpa, file[0]);
            return true ;
        }
        catch(System.Exception e) {
            Console.WriteLine(("Cannot create FileIOPermission: " 
                + file[0] + " " + e));
            fileIOPerm[0] = new FileIOPermission(PermissionState.None);
            file[0] = "";
            return true ;
        }
    } //CreateFilePath
} //FileIOGenerator 

상속 계층 구조

System.Object
   System.Security.CodeAccessPermission
    System.Security.Permissions.FileIOPermission

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

참고 항목

참조

FileIOPermission 멤버
System.Security.Permissions 네임스페이스
FileIOPermissionAccess
FileIOPermissionAttribute

기타 리소스

보안 권한
권한 요청