Retrieve Windows User Profiles  
Author Message
UniqueDisplayName-Adam





PostPosted: Visual C# General, Retrieve Windows User Profiles Top

Does anyone know of a way to programatically retrieve a list of current Windows User Profiles, preferably with the associated user name and SID number.

Thanks in advance.

- Adam



Visual C#12  
 
 
ahmedilyas





PostPosted: Visual C# General, Retrieve Windows User Profiles Top

you could use WMI or you could also use this:

System.Security.Principal.WindowsIdentity.GetCurrent().Owner;

that gets the current user's SID

http://msdn2.microsoft.com/en-us/library/system.security.principal.windowsidentity.getcurrent.aspx

you can get the username also...

System.Security.Principal.WindowsIdentity.GetCurrent().Name;

 

in regards for other Windows User profiles you can do this using WMI. Add a reference to System.Management and import the System.Management and System.Management.Instrumentation namespaces, then you can do this:



using System;
using System.Management;
using System.Windows.Forms;
namespace UserSIDs
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_UserAccount");
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    MessageBox.Show("SID: " + queryObj["SID"].ToString());
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
}

 

you can also get the username by adding this in the foreach loop...

MessageBox.Show("Username: " + queryObj["Name"].ToString());



 
 
guayony





PostPosted: Visual C# General, Retrieve Windows User Profiles Top

Can you show me how to do the same using VB.net
 
 
Ashwin Jayamohan





PostPosted: Visual C# General, Retrieve Windows User Profiles Top

Imports System

Imports System.Management

Imports System.Windows.Forms

Namespace UserSIDs

Public Class MyWMIQuery

Public Shared Sub Main()

Try

Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_UserAccount")

For Each queryObj As ManagementObject In searcher.Get()

MessageBox.Show("SID: " & queryObj("SID").ToString())

Next queryObj

Catch e As ManagementException

MessageBox.Show(e.Message)

End Try

End Sub

End Class

End Namespace