CSharpTest.Net
Find() Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.IO Namespace > FindFile Class > Find Method : Find() Method

Glossary Item Box

Performs the search raising the FileFound event for each entry matching the request

Syntax

Visual Basic (Declaration) 
Public Overloads Sub Find() 
C# 
public void Find()

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