首页 > 代码库 > 文件夹锁定(Source)

文件夹锁定(Source)

文件夹锁定(Source)
private void Lock(string folderPath)
{
    try
    {
        string adminUserName = Environment.UserName;// getting your adminUserName
        DirectorySecurity ds = Directory.GetAccessControl(folderPath);
        FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName, FileSystemRights.FullControl, AccessControlType.Deny);

        ds.AddAccessRule(fsa);
        Directory.SetAccessControl(folderPath, ds);
        MessageBox.Show("Locked");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void UnLock(string folderPath)
{
    try
    {               
        string adminUserName = Environment.UserName;// getting your adminUserName
        DirectorySecurity ds = Directory.GetAccessControl(folderPath);
        FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName, FileSystemRights.FullControl, AccessControlType.Deny);

        ds.RemoveAccessRule(fsa);
        Directory.SetAccessControl(folderPath, ds);
        MessageBox.Show("UnLocked");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

文件夹锁定(Source)