CSharpTest.Net
FileFound Event
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.IO Namespace > FindFile Class : FileFound Event

Glossary Item Box

The event-handler to raise when a file or folder is found.

Syntax

Visual Basic (Declaration) 
Public Event FileFound As EventHandler(Of FindFile.FileFoundEventArgs)

Event Data

The event handler receives an argument of type FindFile.FileFoundEventArgs containing data related to this event. The following FindFile.FileFoundEventArgs properties provide information specific to this event.

PropertyDescription
Attributes Gets the file or folder attributes
CancelEnumeration Gets or sets the Cancel flag to abort the current enumeration
CreationTimeUtc Gets the file or folder CreationTime in Utc
Extension Returns the extenion or String.Empty
FullPath Gets the full path of the file or folder
FullPathUnc Returns the UNC path to the file or folder
IsCompressed Returns true if the file or folder is Compressed
IsDirectory Returns true if the file or folder is Directory
IsEncrypted Returns true if the file or folder is Encrypted
IsHidden Returns true if the file or folder is Hidden
IsOffline Returns true if the file or folder is Offline
IsReadOnly Returns true if the file or folder is ReadOnly
IsReparsePoint Returns true if the file or folder is ReparsePoint
IsSystem Returns true if the file or folder is System
LastAccessTimeUtc Gets the file or folder LastAccessTime in Utc
LastWriteTimeUtc Gets the file or folder LastWriteTime in Utc
Length Gets the length in bytes
Name Returns the file or folder name (with extension)
ParentPath Returns the parent folder's full path
ParentPathUnc Returns the UNC path to the parent folder

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