簡體   English   中英

讀取源權限時如何處理孤立的 NTFS 權限

[英]How to handle Orphaned NTFS Permissions when reading Source Permissions

我遵循此線程中的信息C# Copying a Folder to another destination with Security/Permission Settings

我已經設法讓我的應用程序復制具有權限的文件和文件夾,但是當它遇到具有孤立權限的任何文件夾時,它會完全停止。

像 S-1-5-21-236079 這樣的權限......

有什么建議?

所以這是我的解決方案,在循環中檢查“S-1-5”並跳過它。

    private void PermissionGet(DirectoryInfo Source, DirectoryInfo Destination)
    {
        string Username;
        DirectorySecurity SourceSecurity = Source.GetAccessControl();

        foreach (FileSystemAccessRule Rules in SourceSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
        {
            Username = Rules.IdentityReference.Value;
            if (!Username.Contains("S-1-5"))
            {
                PermissionSet(Username, Rules.FileSystemRights, Rules.AccessControlType, Destination);
            }
        }
    }

    private void PermissionSet(string Username, FileSystemRights Permission, AccessControlType Access, DirectoryInfo Destination)
    {
        try
        {
            DirectorySecurity Security = Destination.GetAccessControl();
            Security.AddAccessRule(new FileSystemAccessRule(Username, Permission, Access));
            Destination.SetAccessControl(Security);
        }
        
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM