2012-01-20 4 views
8

이 작업은 상당히 간단한 문제 여야하지만, 어떤 이유로이 작업을 수행 할 수없는 것 같습니다. 모든 사용자에게 모든 권한을 부여하기 위해 주어진 디렉토리에 대한 사용 권한을 설정합니다. 지금까지 내가 가지고있는 코드는 다음과 같습니다.C# - Windows 7의 모든 사용자에 대한 디렉터리 사용 권한 설정

예외는 발생하지 않지만 아무 것도 발생하지 않습니다. 코드가 실행 된 후 디렉토리 사용 권한을 확인할 때 변경 사항이 표시되지 않습니다.

아이디어가 있으십니까? 사전에

감사합니다,
소니

+0

UAC를 사용하지 않는 위의 코드를 실행하려고 시도 했습니까? – rkosegi

+1

@David - 나는 관리자로서 컴파일 된 exe를 실행하려고 시도했지만 결과에 아무런 영향을주지 않습니다. –

+0

@rkosegi - 어떻게해야합니까? Visual Studio의 설정입니까? –

답변

25

는 또한 변경 사항을 적용 SetAccessControl를 호출해야합니다.

ds = di.GetAccessControl(); 
ds.AddAccessRule(fsar); 
di.SetAccessControl(ds); // nothing happens until you do this 

here을 논의 MSDN에있는 예제가 심하게 자세히 부족한 것 같다. 나는 잘 동작 다음을 얻기 위해이 문서의 코드를 해킹 :

static bool SetAcl() 
{ 
    FileSystemRights Rights = (FileSystemRights)0; 
    Rights = FileSystemRights.FullControl; 

    // *** Add Access Rule to the actual directory itself 
    FileSystemAccessRule AccessRule = new FileSystemAccessRule("Users", Rights, 
           InheritanceFlags.None, 
           PropagationFlags.NoPropagateInherit, 
           AccessControlType.Allow); 

    DirectoryInfo Info = new DirectoryInfo(destinationDirectory); 
    DirectorySecurity Security = Info.GetAccessControl(AccessControlSections.Access); 

    bool Result = false; 
    Security.ModifyAccessRule(AccessControlModification.Set, AccessRule, out Result); 

    if (!Result) 
     return false; 

    // *** Always allow objects to inherit on a directory 
    InheritanceFlags iFlags = InheritanceFlags.ObjectInherit; 
    iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit; 

    // *** Add Access rule for the inheritance 
    AccessRule = new FileSystemAccessRule("Users", Rights, 
           iFlags, 
           PropagationFlags.InheritOnly, 
           AccessControlType.Allow); 
    Result = false; 
    Security.ModifyAccessRule(AccessControlModification.Add, AccessRule, out Result); 

    if (!Result) 
     return false; 

    Info.SetAccessControl(Security); 

    return true; 
} 
+0

고마워, 데이빗. 아직도 운이 없다. 이 목적으로 그룹 "사용자"의 사용이 올바른가요? 나는 관리자로서 exe를 실행 해 보았지만 Windows를 거쳐 폴더에 대한 사용 권한을 확인할 때 아무 일도 일어나지 않는 것으로 보입니다. –

+2

가까이 접근. "사용자"를 "모든 사용자"로 변경하면 "모든 사용자"그룹이 폴더에 추가되지만 사용 권한은 공백으로 표시됩니다. 아무것도주지 않았다. –

+0

이 코드는''Users ''로조차 잘 동작합니다. 즉, 전체 이름은'@ "BUILTIN \ Users"라고 생각하지만 어떤 방식 으로든 작동합니다. –

5

데이비드 헤퍼 넌의 대답은 "사용자"에 대한 권한을 설정려는 IdentityNotMapped 예외와 함께 실패합니다 영어가 아닌 기계에서 작동하지 않습니다 . 다음 코드는 WellKnownSidType.BuiltinUsersSid을 대신하여 모든 곳에서 작동합니다.

static void SetFullControlPermissionsToEveryone(string path) 
{ 
    const FileSystemRights rights = FileSystemRights.FullControl; 

    var allUsers = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null); 

    // Add Access Rule to the actual directory itself 
    var accessRule = new FileSystemAccessRule(
     allUsers, 
     rights, 
     InheritanceFlags.None, 
     PropagationFlags.NoPropagateInherit, 
     AccessControlType.Allow); 

    var info = new DirectoryInfo(path); 
    var security = info.GetAccessControl(AccessControlSections.Access); 

    bool result; 
    security.ModifyAccessRule(AccessControlModification.Set, accessRule, out result); 

    if (!result) 
    { 
     throw new InvalidOperationException("Failed to give full-control permission to all users for path " + path); 
    } 

    // add inheritance 
    var inheritedAccessRule = new FileSystemAccessRule(
     allUsers, 
     rights, 
     InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, 
     PropagationFlags.InheritOnly, 
     AccessControlType.Allow); 

    bool inheritedResult; 
    security.ModifyAccessRule(AccessControlModification.Add, inheritedAccessRule, out inheritedResult); 

    if (!inheritedResult) 
    { 
     throw new InvalidOperationException("Failed to give full-control permission inheritance to all users for " + path); 
    } 

    info.SetAccessControl(security); 
} 
+1

대단히 고마워, 너는 나에게 많은 고통을 덜어 줬다. –

관련 문제