/// <summary> /// Deletes a group /// </summary> /// <param name="groupName"></param> public static void DeleteGroup(string groupName) { int returnCode = Win32GroupInterop.NetLocalGroupDel(null, groupName); switch (returnCode) { case Win32GroupInterop.ReturnCode.S_OK: case Win32GroupInterop.ReturnCode.ERROR_NO_SUCH_ALIAS: case Win32GroupInterop.ReturnCode.NERR_GroupNotFound: break; default: throw new Exception(string.Format("DeleteGroup failed: {0}", returnCode)); } }
/// <summary> /// Removes a user from the named group /// </summary> /// <param name="groupName"></param> /// <param name="userName"></param> public static void RemoveMemberFromGroup(string groupName, string userName) { Win32GroupInterop.LocalGroupMemberInfo memberInfo = new Win32GroupInterop.LocalGroupMemberInfo(); memberInfo.FullName = userName; int returnCode = Win32GroupInterop.NetLocalGroupDelMembers(null, groupName, 3, ref memberInfo, 1); switch (returnCode) { case Win32GroupInterop.ReturnCode.S_OK: case Win32GroupInterop.ReturnCode.ERROR_MEMBER_NOT_IN_ALIAS: break; default: throw new Exception(string.Format("RemoveMemberFromGroup failed: {0}", returnCode)); } }
/// <summary> /// Creates a group /// </summary> /// <param name="groupName"></param> /// <param name="groupComment"></param> public static void CreateGroup(string groupName, string groupComment) { Win32GroupInterop.LocalGroupInfo groupInfo = new Win32GroupInterop.LocalGroupInfo(); groupInfo.Name = groupName; groupInfo.Comment = groupComment; int returnCode = Win32GroupInterop.NetLocalGroupAdd(null, 1, ref groupInfo, 0); switch (returnCode) { case Win32GroupInterop.ReturnCode.S_OK: case Win32GroupInterop.ReturnCode.ERROR_ALIAS_EXISTS: case Win32GroupInterop.ReturnCode.NERR_GroupExists: break; default: throw new Exception(string.Format("CreateGroup failed: {0}", returnCode)); } }