Skip to content

Instantly share code, notes, and snippets.

@roufamatic
Forked from anonymous/Program.cs
Created January 15, 2014 19:31
Show Gist options
  • Save roufamatic/8442829 to your computer and use it in GitHub Desktop.
Save roufamatic/8442829 to your computer and use it in GitHub Desktop.
using System;
using System.DirectoryServices;
namespace EmailAddressTest
{
class Program
{
static void Main(string[] args)
{
do
{
Console.WriteLine();
Console.WriteLine("------------------");
String username = Environment.UserName;
Console.WriteLine("Enter the username in question [leave blank for {0}]:", username);
String newUsername = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(newUsername)) username = newUsername;
// get a DirectorySearcher object
var entry = new DirectoryEntry();
var search = new DirectorySearcher(entry);
// specify the search filter
search.Filter = "(&(objectClass=user)(anr=" + username + "))";
// specify which property values to return in the search
search.PropertiesToLoad.Add("givenName"); // first name
search.PropertiesToLoad.Add("sn"); // last name
search.PropertiesToLoad.Add("mail"); // smtp mail address
// perform the search
SearchResult result = search.FindOne();
if (result == null)
{
Console.WriteLine("*** user {0} not found", username);
}
else
{
Console.WriteLine("Name = {0} {1}", result.Properties["givenName"][0], result.Properties["sn"][0]);
Console.WriteLine("Email = {0}", result.Properties["mail"][0]);
}
Console.WriteLine("press 'y' to try again, any other key to exit.");
} while (Console.ReadKey().KeyChar.ToString().ToLower() == "y");
}
}
}
@roufamatic
Copy link
Author

Sorry, can't help -- haven't touched Windows in almost 4 years. Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment