Creates a FindFile instance.
Syntax
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.NET | Copy 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