2010-06-25 3 views
0

기존 폴더에서 새로 생성 된 NTFS 사용 권한 정보를 복사하는 방법을 찾았습니다. 작업을 수행하고 있는지 확실하지 않습니다. 어쩌면 하나의 방법에서 모양과 몇 가지 의견을 제공 할 수 있습니다 : 당신의 도움에 대한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); 
} 

감사합니다, 다니엘

답변

1

그것은 바로 NTFS 권한을 복사하는 것보다 약간 더 않습니다. 또한 파일의 속성을 복사합니다. 상속 된 사용 권한을 복사할지 여부는 확실하지 않지만 한 번 실행하면 찾을 수 있습니다.

사용 권한을 복사하려면 특수 사용 권한이 필요합니다 (물론 관리자는 이러한 권한을가집니다).이 메서드를 실행하는 프로세스에 해당 개체에 대한 쿼리,보기 및 사용 권한을 설정하는 데 필요한 권한이 있는지 확인하십시오.

+0

잘 복사 권한 _and_ attributes는 내가 달성하기를 원하는 동작과 정확히 같습니다. - 죄송합니다. 전에 말하지 않으 셨습니다 ...-) 예, 응용 프로그램은 관리자 계정이 사용되도록합니다. – dhh

2

파일의 ACL과 속성을 복사하기 위해 OP 제안 패턴을 따라했는데 우리 응용 프로그램에서 몇 가지 문제점을 발견했습니다. 바라기를이 정보는 다른 사람을 돕는다.

위의 그림과 같이 File.SetAccessControl() 메서드를 사용하면 MSDN에 따르면 작동하지 않습니다.

SetAccessControl 메서드는 개체 생성 후 이 수정 된 FileSecurity 개체 만 유지합니다. FileSecurity 개체 이 수정되지 않은 경우 파일에 유지되지 않습니다. 따라서 한 파일에서 FileSecurity 개체를 검색 할 수 없으며 은 동일한 개체를 다른 파일로 다시 적용합니다..

대상 파일에 대해 FileSecurity 개체를 새로 만들고이 개체에 원본 FileSecurity 복사본을 할당해야합니다.

다음은 Google 앱에서 작동하는 패턴의 예입니다.

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); 
    } 

첫 번째 문제를 수정 한 후 소유자 특성이 복사되지 않는 것으로 나타났습니다. 대신 대상 파일은이 경우 Administrator가 소유합니다. 이 프로세스가 SE_RESTORE_NAME 권한을 사용 가능하게 설정해야합니다. pinvoke.net의 AdjustTokenPrivileges에서 설정 권한을 쉽게 사용할 수있는 완전한 권한 클래스를 확인하십시오.

우리가 처리해야했던 마지막 사항은 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); 
    } 

는 우리의 운명은 우리가 RelaunchWithAdministratorRights(args)Main 방법에서 args 매개 변수를 전달해야하는 콘솔 응용 프로그램이었다. 비 콘솔 앱은 대신 null을 전달할 수 있습니다. 우리는 같이 catch 블록 내에서 RelaunchWithAdministratorRights 전화 :

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

우리의 응용 프로그램에서 우리는 권한이 부족 응용 프로그램의 인스턴스를 종료 RelaunchWithAdministratorRights에 대한 호출 후 반환한다. 앱에 따라 도중에서 throw을 선호 할 수 있습니다.그럼에도 불구하고 RelaunchWithAdministratorRights에서 복귀 한 후이 인스턴스가 처리를 계속하는 것을 원하지 않습니다.

즐기십시오!

+0

아주 오래된 질문입니다.하지만 많은 결과를 공유해 주신 Gyle에게 감사드립니다. – dhh

관련 문제