Click here to Skip to main content
15,885,546 members
Home / Discussions / C#
   

C#

 
GeneralRe: Cancelling a background worker Pin
Muammar©6-Oct-07 1:33
Muammar©6-Oct-07 1:33 
QuestionCopying files to clipboard?? Pin
Muammar©5-Oct-07 22:19
Muammar©5-Oct-07 22:19 
AnswerRe: Copying files to clipboard?? Pin
pmarfleet5-Oct-07 22:37
pmarfleet5-Oct-07 22:37 
GeneralRe: Copying files to clipboard?? Pin
Muammar©5-Oct-07 22:53
Muammar©5-Oct-07 22:53 
GeneralRe: Copying files to clipboard?? Pin
User 66586-Oct-07 2:11
User 66586-Oct-07 2:11 
GeneralRe: Copying files to clipboard?? Pin
Muammar©6-Oct-07 11:31
Muammar©6-Oct-07 11:31 
QuestionUsers in local users and groups Pin
Surya Ayyagari5-Oct-07 22:02
Surya Ayyagari5-Oct-07 22:02 
AnswerRe: Users in local users and groups [modified] Pin
Hessam Jalali5-Oct-07 23:57
Hessam Jalali5-Oct-07 23:57 
you can do that using WMI through System.Management namespace (you must add it as reference)

you can query Win32_UserAccount and Win32_UserGroup from WMI for collecting your data

maybe this is not the best solution but it's going to work Smile | :)

here is the code

public class UserManagements
        {
            public class GroupUser
            {
                const string PATTERNNAME = ".*Name=\"(?'name'.*)\".*";
                const string PATTERNDOMAIN = ".*Domain=\"(?'domain'.*)\",.*";

                public readonly string groupName;
                public readonly string partName;

                public readonly string groupDomain;
                public readonly string partDomain;

                public GroupUser(string groupComponent, string partComponent)
                {
                    this.groupName = Regex.Replace(groupComponent, PATTERNNAME, "${name}");
                    this.partName = Regex.Replace(partComponent, PATTERNNAME, "${name}");

                    this.groupDomain = Regex.Replace(groupComponent, PATTERNDOMAIN, "${domain}");
                    this.partDomain = Regex.Replace(partComponent, PATTERNDOMAIN, "${domain}");
                }

            }

            public class UserAccount
            {
                public readonly int AccountType;
                public readonly string Caption;
                public readonly string Description;
                public readonly bool Disabled;
                public readonly string Domain;
                public readonly string FullName;

                public readonly bool LocalAccount;
                public readonly bool Lockout;
                public readonly string Name;
                public readonly bool PasswordChangeable;
                public readonly bool PasswordExpires;
                public readonly bool PasswordRequired;
                public readonly string SID;
                public readonly int SIDType;
                public readonly string Status;

                public UserAccount(ManagementObject userMO)
                {
                    this.AccountType = Convert.ToInt32(userMO.Properties["AccountType"].Value);
                    this.Caption = userMO.Properties["Caption"].Value as string;
                    this.Description = userMO.Properties["Description"].Value as string;
                    this.Disabled = Convert.ToBoolean(userMO.Properties["AccountType"].Value);
                    this.Domain = userMO.Properties["Domain"].Value as string;
                    this.FullName = userMO.Properties["FullName"].Value as string;

                    this.LocalAccount = Convert.ToBoolean(userMO.Properties["LocalAccount"].Value);
                    this.Lockout = Convert.ToBoolean(userMO.Properties["Lockout"].Value);
                    this.Name = userMO.Properties["Name"].Value as string;
                    this.PasswordChangeable = Convert.ToBoolean(userMO.Properties["PasswordChangeable"].Value);
                    this.PasswordExpires = Convert.ToBoolean(userMO.Properties["PasswordExpires"].Value);
                    this.PasswordRequired = Convert.ToBoolean(userMO.Properties["PasswordRequired"].Value);
                    this.SID = userMO.Properties["SID"].Value as string;
                    this.SIDType = Convert.ToInt32(userMO.Properties["SIDType"].Value);
                    this.Status = userMO.Properties["Status"].Value as string;
                }
            }

            public static GroupUser[] GetGroupUsers()
            {
                List <GroupUser> groupUsers = new List <GroupUser>();
                ManagementObjectSearcher mos = new ManagementObjectSearcher(new SelectQuery("Win32_GroupUser"));
                ManagementObjectCollection moc = mos.Get();
                foreach (ManagementObject mo in moc)
                {
                    string groupComponent = mo.Properties["GroupComponent"].Value as string;
                    string partComponent = mo.Properties["PartComponent"].Value as string;

                    GroupUser gu = new GroupUser(groupComponent, partComponent);
                    groupUsers.Add(gu);
                }
                return groupUsers.ToArray();
            }

            public static UserAccount[] GetUserAccounts()
            {
                List <UserAccount> userAccounts = new List <UserAccount>();
                ManagementObjectSearcher mos = new ManagementObjectSearcher(new SelectQuery("Win32_UserAccount"));
                ManagementObjectCollection moc = mos.Get();
                foreach (ManagementObject mo in moc)
                    userAccounts.Add(new UserAccount(mo));

                return userAccounts.ToArray();
            }
        }


you can use it through GetUserGroups and GetUserAccounts methods.

hope the post would be useful

Smile | :)


-- modified at 6:02 Saturday 6th October, 2007
QuestionSelecting an item in a listbox control Pin
Muammar©5-Oct-07 21:43
Muammar©5-Oct-07 21:43 
AnswerRe: Selecting an item in a listbox control Pin
Hessam Jalali5-Oct-07 23:00
Hessam Jalali5-Oct-07 23:00 
GeneralRe: Selecting an item in a listbox control Pin
Muammar©5-Oct-07 23:17
Muammar©5-Oct-07 23:17 
QuestionGetting error to send mail in asp.net 2.0 Pin
Rajesh Ji5-Oct-07 21:25
Rajesh Ji5-Oct-07 21:25 
AnswerRe: Getting error to send mail in asp.net 2.0 Pin
Christian Graus5-Oct-07 22:35
protectorChristian Graus5-Oct-07 22:35 
AnswerRe: Getting error to send mail in asp.net 2.0 Pin
Vasudevan Deepak Kumar5-Oct-07 23:13
Vasudevan Deepak Kumar5-Oct-07 23:13 
QuestionDelegates Pin
.NET- India 5-Oct-07 21:24
.NET- India 5-Oct-07 21:24 
AnswerRe: Delegates Pin
pmarfleet5-Oct-07 22:12
pmarfleet5-Oct-07 22:12 
AnswerRe: Delegates Pin
Christian Graus5-Oct-07 23:46
protectorChristian Graus5-Oct-07 23:46 
QuestionCopy and Move with FileSystemWatch in C# Pin
Abhijit Jana5-Oct-07 21:11
professionalAbhijit Jana5-Oct-07 21:11 
AnswerRe: Copy and Move with FileSystemWatch in C# Pin
pmarfleet5-Oct-07 22:25
pmarfleet5-Oct-07 22:25 
GeneralRe: Copy and Move with FileSystemWatch in C# Pin
Abhijit Jana6-Oct-07 1:01
professionalAbhijit Jana6-Oct-07 1:01 
AnswerRe: Copy and Move with FileSystemWatch in C# Pin
Dave Kreskowiak6-Oct-07 3:14
mveDave Kreskowiak6-Oct-07 3:14 
QuestionQuestion about Content Pipeline Files.... Pin
MMaines20055-Oct-07 16:22
MMaines20055-Oct-07 16:22 
Question"Or" Pin
MasterSharp5-Oct-07 12:24
MasterSharp5-Oct-07 12:24 
AnswerRe: "Or" Pin
Anthony Mushrow5-Oct-07 12:36
professionalAnthony Mushrow5-Oct-07 12:36 
GeneralRe: "Or" Pin
pmarfleet5-Oct-07 12:56
pmarfleet5-Oct-07 12:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.