2

WinXP, Vista 및 7 운영 체제를 대상으로하는 C# 응용 프로그램을 만들고 있습니다.프로그래밍 방식으로 로컬 그룹에 로컬 사용자 추가

하나의 기능은 프로그래밍 방식으로 사용자에게 그룹 설정을 추가, 제거, 수정할 수 있다는 것입니다.

어떻게해야할까요?

WMI에서이 작업을 수행 할 수 있습니까? 현재 Windows7의

이 코드를

DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",Computer"); 
localMachine.Properties["member"].Add("Chevi"); 
localMachine.CommitChanges(); 
localMachine.Close(); 

을 테스트하기 위해 노력하고 있으며이 오류

침 것을 사용하고 주로 사용자를 얻기 위해 WMI를 사용하여 내 코드 ..


디렉터리 속성을 캐시에서 찾을 수 없습니다.

나는 속성 모음을 열거 시도하고 나는 시스템을 net 명령을 호출하여이

OperatingSystem 

OperatingSystemVersion 

Owner 

Division 

ProcessorCount 

Processor 

Name 

답변

2

로컬 그룹을 사용하는 경우, 당신은이 작업을 수행 할 수있어. 예를 들어, 그룹에 사용자를 추가, 당신은 전화 것 : 명령 프롬프트에서

net localgroup MyGroup /add SomeUser 

유형 net help localgroup 더 많은 정보를 원하시면.

WMI를 사용하여이 작업을 수행 할 수도 있습니다. 이것은 VBScript를하지만, .NET 또는 선호하는 프로그래밍 툴킷에 적용 할 수 있습니다 :

Dim oComputer 
Computer = "computername" 
Groupname = "Administrators" 
ObjectToAdd = "Administrator" 

' Bind to the computer. 
Set oComputer = GetObject("WinNT://" & Computer & ",computer") 

' get group object 
Dim oGroup 
Set oGroup = oComputer.GetObject("group", GroupName) 

' Add the user object to the group. 
oGroup.Add "WinNT://" & Computer & "/" & ObjectToAdd 

제공 : 매트 히크, http://www.tech-archive.net/Archive/WinXP/microsoft.public.windowsxp.wmi/2004-04/0007.html

+0

예 제발 WMI !! – jaysonragasa

+0

WMI에서 그룹에 사용자를 추가 할 수 있습니까? 업데이트 쿼리가 있습니까? : D – jaysonragasa

+0

나는 그물 명령에 갈 것이다. WMI는 자주 중단되며 느립니다. NET 명령의 속도는 확실하지 않지만 최악의 경우에는 더 안정적입니다. –

0

나는 또한 C#을 사용, 비주얼 스튜디오 2010 하나의 Windows 응용 프로그램을 개발했습니다. 이것은 프로그램의 작동 버전으로 기존 사용자를 특정 그룹에 추가합니다.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.DirectoryServices; 

namespace Utility_Add_User_To_Group { 

    public partial class Form1 : Form { 

     public Form1() { 
      InitializeComponent(); 
     } 

     private void btn_Add_Click(object sender, EventArgs e) { 
      string usr, grp; 
      usr = txt_UserName.Text; 
      grp = txt_GroupName.Text; 

      add(usr, grp); 
      groupBox2.Visible=true; 
     } 

     private void add(string usr, string grp) { 
      bool flagUsr, flagGrp; 
      try { 
       DirectoryEntry AD = new DirectoryEntry("WinNT://" +Environment.MachineName + ",computer"); 
       DirectoryEntry group, user; 

       group = AD.Children.Find(grp, "group"); 
       user = AD.Children.Find(usr, "user"); 
       if (user != null) { 
        label3.Text += "User Name Exists!!!"; 
        flagUsr = true; 
       } else { 
        label3.Text += "Sorry, No Such User Name Found!!!"; 
        flagUsr = false; 
       } 

       if (group != null) { 
        label4.Text += "Group Exists!!!"; 
        flagGrp = true; 
       } else { 
        label4.Text += "Sorry, Group Does Not Exists!!!"; 
        flagGrp= false; 
       } 

       if(flagGrp == true && flagUsr == true) { 
        group.Invoke("Add", new object[] { user.Path.ToString() }); 
        label5.Text += "Congratulations!!! User has been added to the group"; 
       } else { 
        label5.Text += "Error Happened!!! User could not be added to the group!!!"; 
       } 
      } catch (Exception e) { 
       label6.Text +=e.Message.ToString(); 
       label6.Visible= true; 
      } 
      } 

     private void btn_Clear_Click(object sender, EventArgs e) { 
      normal(); 
     } 
     private void normal() { 
      txt_GroupName.Text=""; 
      txt_UserName.Text=""; 
      txt_UserName.Focus(); 

      groupBox2.Visible=false; 
     } 
     } 
    } 
관련 문제