CSharpTest.Net
ReplacePermissions Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Utils Namespace > FileUtils Class : ReplacePermissions Method

filepath
sidType
allow

Glossary Item Box

Removes any existing access for the user SID supplied and adds the specified rights

Syntax

Visual Basic (Declaration) 
Public Shared Sub ReplacePermissions( _
   ByVal filepath As String, _
   ByVal sidType As WellKnownSidType, _
   ByVal allow As FileSystemRights _
) 
C# 
public static void ReplacePermissions( 
   string filepath,
   WellKnownSidType sidType,
   FileSystemRights allow
)

Parameters

filepath
sidType
allow

Example

Library/Library.Test/TestFileUtils.cs

C#Copy Code
string tempFile = Path.GetTempFileName();
FileSystemRights rights;
try
{
    FileUtils.ReplacePermissions(tempFile, WellKnownSidType.WorldSid, FileSystemRights.Read);
    rights = FileUtils.GetPermissions(tempFile, WellKnownSidType.WorldSid);
    Assert.AreEqual(FileSystemRights.Read, FileSystemRights.Read & rights);

    FileUtils.ReplacePermissions(tempFile, WellKnownSidType.WorldSid, 0);
    rights = FileUtils.GetPermissions(tempFile, WellKnownSidType.WorldSid);
    Assert.AreEqual(0, (int)rights);

    FileUtils.GrantFullControlForFile(tempFile, WellKnownSidType.WorldSid);
    rights = FileUtils.GetPermissions(tempFile, WellKnownSidType.WorldSid);
    Assert.AreEqual(FileSystemRights.FullControl, rights);

    FileUtils.ReplacePermissions(tempFile, WellKnownSidType.WorldSid, FileSystemRights.Read);
    rights = FileUtils.GetPermissions(tempFile, WellKnownSidType.WorldSid);
    Assert.AreEqual(FileSystemRights.Read, FileSystemRights.Read & rights);

    FileUtils.GrantFullControlForFile(tempFile, WellKnownSidType.WorldSid);
    rights = FileUtils.GetPermissions(tempFile, WellKnownSidType.WorldSid);
    Assert.AreEqual(FileSystemRights.FullControl, rights);

    FileUtils.ReplacePermissions(tempFile, WellKnownSidType.WorldSid, 0);
    rights = FileUtils.GetPermissions(tempFile, WellKnownSidType.WorldSid);
    Assert.AreEqual(0, (int)rights);
}
finally
{
    File.Delete(tempFile);
}
VB.NETCopy Code
Dim tempFile As String = Path.GetTempFileName()
Dim rights As FileSystemRights
Try
    FileUtils.ReplacePermissions(tempFile, WellKnownSidType.WorldSid, FileSystemRights.Read)
    rights = FileUtils.GetPermissions(tempFile, WellKnownSidType.WorldSid)
    Assert.AreEqual(FileSystemRights.Read, FileSystemRights.Read And rights)

    FileUtils.ReplacePermissions(tempFile, WellKnownSidType.WorldSid, 0)
    rights = FileUtils.GetPermissions(tempFile, WellKnownSidType.WorldSid)
    Assert.AreEqual(0, DirectCast(rights, Integer))

    FileUtils.GrantFullControlForFile(tempFile, WellKnownSidType.WorldSid)
    rights = FileUtils.GetPermissions(tempFile, WellKnownSidType.WorldSid)
    Assert.AreEqual(FileSystemRights.FullControl, rights)

    FileUtils.ReplacePermissions(tempFile, WellKnownSidType.WorldSid, FileSystemRights.Read)
    rights = FileUtils.GetPermissions(tempFile, WellKnownSidType.WorldSid)
    Assert.AreEqual(FileSystemRights.Read, FileSystemRights.Read And rights)

    FileUtils.GrantFullControlForFile(tempFile, WellKnownSidType.WorldSid)
    rights = FileUtils.GetPermissions(tempFile, WellKnownSidType.WorldSid)
    Assert.AreEqual(FileSystemRights.FullControl, rights)

    FileUtils.ReplacePermissions(tempFile, WellKnownSidType.WorldSid, 0)
    rights = FileUtils.GetPermissions(tempFile, WellKnownSidType.WorldSid)
    Assert.AreEqual(0, DirectCast(rights, Integer))
Finally
    File.Delete(tempFile)
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