CSharpTest.Net
FindFile Constructor(String,String,Boolean,Boolean)
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.IO Namespace > FindFile Class > FindFile Constructor : FindFile Constructor(String,String,Boolean,Boolean)

rootDirectory
filePattern
recursive
includeFolders

Glossary Item Box

Creates a FindFile instance.

Syntax

Visual Basic (Declaration) 
Public Function New( _
   ByVal rootDirectory As String, _
   ByVal filePattern As String, _
   ByVal recursive As Boolean, _
   ByVal includeFolders As Boolean _
)
C# 
public FindFile( 
   string rootDirectory,
   string filePattern,
   bool recursive,
   bool includeFolders
)

Parameters

rootDirectory
filePattern
recursive
includeFolders

Example

Library/Library.Test/TestFindFile.cs

C#Copy Code
string child2 = Path.Combine(TestFolder, @"child1\child2");
            
DirectorySecurity acl = Directory.GetAccessControl(child2);
byte[] original = acl.GetSecurityDescriptorBinaryForm();
acl.AddAccessRule(
    new FileSystemAccessRule(
        new SecurityIdentifier(WellKnownSidType.WorldSid, null),
        FileSystemRights.ListDirectory,
        AccessControlType.Deny)
    );
Directory.SetAccessControl(child2, acl);
try
{
    //By default it ignores AccessDenied
    FindFile ff = new FindFile(child2, "*", false, false);
    ff.FileFound += (o, e) => Assert.Fail("Should not find a file");
    ff.Find();

    //Now raise the AccessDenied
    ff.RaiseOnAccessDenied = true;
    try
    {
        ff.Find();
        Assert.Fail("Should throw Access Denied.");
    }
    catch(System.ComponentModel.Win32Exception we)
    { Assert.AreEqual(5, we.NativeErrorCode); }
}
finally
{
    acl.SetSecurityDescriptorBinaryForm(original, AccessControlSections.All);
    Directory.SetAccessControl(child2, acl);
}
VB.NETCopy Code
Dim child2 As String = Path.Combine(TestFolder, "child1\child2")

Dim acl As DirectorySecurity = Directory.GetAccessControl(child2)
Dim original As Byte() = acl.GetSecurityDescriptorBinaryForm()
acl.AddAccessRule(New FileSystemAccessRule(New SecurityIdentifier(WellKnownSidType.WorldSid, Nothing), FileSystemRights.ListDirectory, AccessControlType.Deny))
Directory.SetAccessControl(child2, acl)
Try
    'By default it ignores AccessDenied
    Dim ff As New FindFile(child2, "*", False, False)
    ff.FileFound += Function(o, e) Assert.Fail("Should not find a file")
    ff.Find()

    'Now raise the AccessDenied
    ff.RaiseOnAccessDenied = True
    Try
        ff.Find()
        Assert.Fail("Should throw Access Denied.")
    Catch we As System.ComponentModel.Win32Exception
        Assert.AreEqual(5, we.NativeErrorCode)
    End Try
Finally
    acl.SetSecurityDescriptorBinaryForm(original, AccessControlSections.All)
    Directory.SetAccessControl(child2, acl)
End Try

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys