AddPathList Method

FileIOPermission.AddPathList Method

Adds access for the specified files and directories to the existing state of the permission.

Overload List

Adds access for the specified file or directory to the existing state of the permission.

[Visual Basic] Overloads Public Sub AddPathList(FileIOPermissionAccess, String)
[C#] public void AddPathList(FileIOPermissionAccess, string);
[C++] public: void AddPathList(FileIOPermissionAccess, String*);
[JScript] public function AddPathList(FileIOPermissionAccess, String);

Adds access for the specified files and directories to the existing state of the permission.

[Visual Basic] Overloads Public Sub AddPathList(FileIOPermissionAccess, String())
[C#] public void AddPathList(FileIOPermissionAccess, string[]);
[C++] public: void AddPathList(FileIOPermissionAccess, String[]);
[JScript] public function AddPathList(FileIOPermissionAccess, String[]);

Example

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of AddPathList. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' 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


[C#] 
// 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;        
    }

[C++] 
// 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(S"********************************************************\n");

        FileIOPermission* fileIOPerm1;
        Console::WriteLine(S"Creating a FileIOPermission with AllAccess rights for 'C:\\Examples\\Test\\TestFile.txt");
        fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess::AllAccess, S"C:\\Examples\\Test\\TestFile.txt");
        Console::WriteLine(S"Adding 'C:\\Temp' to the write access list, and \n 'C:\\Examples\\Test' to read access.");
        fileIOPerm1->AddPathList(FileIOPermissionAccess::Write, S"C:\\Temp");
        fileIOPerm1->AddPathList(FileIOPermissionAccess::Read, S"C:\\Examples\\Test");
        String* paths[] = fileIOPerm1->GetPathList(FileIOPermissionAccess::Read);
        Console::WriteLine(S"Read access before SetPathList = ");
        IEnumerator* myEnum = paths->GetEnumerator();
        while (myEnum->MoveNext())
        {
            String* path = __try_cast<String*>(myEnum->Current);
            Console::WriteLine(S"\t{0}", path);
        }
        Console::WriteLine(S"Setting the read access list to \n'C:\\Temp'");
        fileIOPerm1->SetPathList(FileIOPermissionAccess::Read, S"C:\\Temp");
        paths = fileIOPerm1->GetPathList(FileIOPermissionAccess::Read);
        Console::WriteLine(S"Read access list after SetPathList = ");
        IEnumerator* myEnum1 = paths->GetEnumerator();
        while (myEnum1->MoveNext())
        {
            String* path = __try_cast<String*>(myEnum1->Current);
            Console::WriteLine(S"\t{0}", path);
        }

        paths = fileIOPerm1->GetPathList(FileIOPermissionAccess::Write);
        Console::WriteLine(S"Write access list after SetPathList = ");
        IEnumerator* myEnum2 = paths->GetEnumerator();
        while (myEnum2->MoveNext())
        {
            String* path = __try_cast<String*>(myEnum2->Current);
            Console::WriteLine(S"\t{0}", path);
        }

        Console::WriteLine(S"Write access = \n{0}", fileIOPerm1->GetPathList(FileIOPermissionAccess::AllAccess));

    }
    catch (ArgumentException* e)
    {
        // FileIOPermissionAccess.AllAccess can not be used as a parameter for GetPathList.
        Console::WriteLine(S"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;        
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

FileIOPermission Class | FileIOPermission Members | System.Security.Permissions Namespace

Show:
© 2016 Microsoft