문제

이것은 정말 쉬워야하지만 어떤 이유로는 그렇지 않은 것 같습니다. 현재 컴퓨터가 특정 그룹의 구성원인지 광고를 요청하고 싶습니다. 직접 멤버십은 괜찮습니다.

그룹에는 8 개의 PC 만 포함되어 있으며 30을 넘어 성장할 가능성은 거의 없습니다.

C# 코드 예제 감사합니다!

도움이 되었습니까?

해결책

다음은 다음을 사용하는 예제 방법입니다 System.DirectoryServices 네임 스페이스 :

public bool BelongsToGroup(string computerName, string groupName, string domain)
{
   PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain);

   ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(domainContext, computerName);

   foreach (Principal result in computer.GetGroups())
   {
      if (result.Name == groupName)
      {
         return true;
      }
   }

  return false;
}

그래서 당신은 이것을 다음과 같이 부를 수 있습니다.

string computerName = Environment.MachineName;
string groupName = "Group Name";
string domainName = "Domain Name";
bool test = BelongsToGroup(computerName, groupName, domainName);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top