簡體   English   中英

如何復制ntfs權限

[英]How to copy ntfs permissions

我找到了一種將ntfs權限信息從一個現有文件夾復制到新創建的文件夾的方法-我不確定它是否正在執行應做的工作。 也許可以看看該方法並給出一些評論:

private static void CopySecurityInformation(String source, String dest)
{
    FileSecurity fileSecurity = File.GetAccessControl(
        source,
        AccessControlSections.All);
    FileAttributes fileAttributes = File.GetAttributes(source);
    File.SetAccessControl(dest, fileSecurity);
    File.SetAttributes(dest, fileAttributes);
}

謝謝您的幫助,丹尼爾

我嘗試遵循OP建議的模式來復制文件的ACL和屬性,但在我們的應用程序中發現了一些問題。 希望這些信息對其他人有幫助。

根據MSDN,使用上面顯示的File.SetAccessControl()方法將不起作用。

SetAccessControl方法僅保留在對象創建后已修改的FileSecurity對象。 如果未修改FileSecurity對象,則不會將其持久保存到文件中。 因此, 不可能從一個文件中檢索FileSecurity對象並將同一對象重新應用於另一文件

必須為目標文件創建一個新的FileSecurity對象,並為其分配源FileSecurity對象的副本。

這是可以在我們的應用中使用的模式的示例。

    public static void CopyFile(string source, string destination)
    {
        // Copy the file
        File.Copy(source, destination, true);

        // Get the source file's ACLs
        FileSecurity fileSecuritySource = File.GetAccessControl(source, AccessControlSections.All);
        string sddlSource = fileSecuritySource.GetSecurityDescriptorSddlForm(AccessControlSections.All);

        // Create the destination file's ACLs
        FileSecurity fileSecurityDestination = new FileSecurity();
        fileSecurityDestination.SetSecurityDescriptorSddlForm(sddlSource);

        // Set the destination file's ACLs
        File.SetAccessControl(destination, fileSecurityDestination);

        // copy the file attributes now
        FileAttributes fileAttributes = File.GetAttributes(source);
        File.SetAttributes(destination, fileAttributes);
    }

修復第一個問題后,我們發現沒有復制owner屬性。 相反,在本例中,目標文件歸管理員所有。 為了解決這個問題,該過程需要啟用SE_RESTORE_NAME特權。 在pinvoke.net上查看AdjustTokenPrivileges ,以獲得完整的Privileges類,該類使設置權限變得容易。

我們必須處理的最后一件事是UAC。 如果用戶在UAC下運行,我們需要以管理權限重新啟動我們的應用程序。 這是我們應用程序中的另一個示例,該示例涉及以提升的特權重新啟動應用程序。

    private static void RelaunchWithAdministratorRights(string[] args)
    {
        // Launch as administrator
        ProcessStartInfo processStartInfo = new ProcessStartInfo();
        processStartInfo.UseShellExecute = true;
        processStartInfo.WorkingDirectory = Environment.CurrentDirectory;
        string executablePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
        processStartInfo.FileName = executablePath;
        processStartInfo.Verb = "runas";

        if (args != null && args.Count() > 0)
        {
            string arguments = args[0];
            for (int i = 1; i < args.Count(); i++)
                arguments += " " + args[i];
            processStartInfo.Arguments = arguments;
        }

        try
        {
            using (Process exeProcess = Process.Start(processStartInfo))
            {
                exeProcess.WaitForExit();
            }
        }
        catch
        {
            // The user refused to allow privileges elevation. Do nothing and return directly ...
        }

        Environment.Exit(0);
    }

我們的是一個控制台應用程序,因此我們需要將args參數從Main方法傳遞給RelaunchWithAdministratorRights(args) 非控制台應用程序可以改為傳遞null。 我們從catch塊中調用RelaunchWithAdministratorRights ,如下所示:

        try
        {
            ...
        }
        catch (SecurityException)
        {
            RelaunchWithAdministratorRights(args);
            return;
        }

在我們的應用程序中,我們只是在調用RelaunchWithAdministratorRights之后返回,以退出缺少特權的應用程序實例。 根據您的應用程序,您可能更喜歡throw自己的出路。 無論如何,從RelaunchWithAdministratorRights返回后,您都不希望該實例繼續處理。

請享用!

它所做的不僅僅是復制NTFS權限。 它還會復制文件的屬性。 我不確定它是否復制繼承的權限,但是只運行一次就可以發現。

請注意,復制權限本身需要特殊權限(當然,管理員擁有這些權限),請確保運行此方法的進程具有查詢,查看和設置這些對象的所需權限。

暫無
暫無

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

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