Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi

I have a code to retrieve all users who are active,but this is a console application.

i want to convert this app to dll and call this dll when ever i click the button from windows app.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
class Program
{
    static void Main()
    {
        DirectorySearcher searcher = new DirectorySearcher();
        searcher.Filter = "(&(objectCategory=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
        foreach (SearchResult result in searcher.FindAll())
        Console.WriteLine(result.GetDirectoryEntry().Properties["sAMAccountName"].Value);
        Console.ReadLine();
    }
}
Posted

1 solution

Create a new DLL project. Add a new class to it and add a static method that returns an IEnumerable<string>. Or have it fire an event which will have the string as an arg - that way you don't have to wait for the entire search to complete before you can start processing the results.

Here's the former approach:

C#
public static IEnumerable<object> GetSearchResults()
{
    Collection<object> results = new Collection<object>();

    DirectorySearcher searcher = new DirectorySearcher();
    searcher.Filter = "(&(objectCategory=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
    foreach (SearchResult result in searcher.FindAll())
    {
        var value = result.GetDirectoryEntry().Properties["sAMAccountName"].Value;
        results.Add(value);
    }

    return results;
}


And here's the other option I mentioned:

C#
public event EventHandler<SearchEventArgs> ItemFound;

public void StartSearch()
{
    DirectorySearcher searcher = new DirectorySearcher();
    searcher.Filter = "(&(objectCategory=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
    foreach (SearchResult result in searcher.FindAll())
    {
        var value = result.GetDirectoryEntry().Properties["sAMAccountName"].Value;
        if (ItemFound != null)
        {
            ItemFound(this, new SearchEventArgs() { Value = value });
        }
    }
}


where SearchEventArgs is:

C#
public class SearchEventArgs : EventArgs
{
    public object Value { get; set; }
}
 
Share this answer
 
v3
Comments
fjdiewornncalwe 24-Feb-11 22:13pm    
OP's Comment moved from answer:
Thanks a lot Nishant,i already have a class library file,i have to include this method in to my code ,so when ever i click the button am going to trigger and retrieve the use
shan1395 24-Feb-11 22:14pm    
thanks for your suggesting and code,the reason i want to convert this.

Already i have DLL file,i wanna add this one of my method in my existing file

this is the code,i have to include this method in that code



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace ActiveDir
{
public class Employee
{
// instance variables
public String name, loginname, firstname;
public String surname;
public string title;
public string company, Org;
public string email, useraccountcontrol;
public string mobile, telephonenumber, pager;
public string city, state, country, postalcode;

// ...
// Constructors

public String FullName()
{

return String.Format("{0}. {1}. {2}. {3}. {4}. {5}. {6}. {7}. {8}. {9}. {10}. {11}. {12}. {13}. {14}.{15} ", name, title, company, surname, email, pager, mobile, telephonenumber, city, state, postalcode, country, loginname, firstname, useraccountcontrol, Org).Trim();

}
public static implicit operator String(Employee value)
{
return value.ToString();
}

public Employee()
{
}
}

public class ActiveDirSearch
{
public ActiveDirSearch()
{ }

public Employee SearchUser(string username)
{

Employee employee = new Employee();


try
{
DirectoryEntry entry = new DirectoryEntry("GC://Test", "Test\\Test", "Ld@Test", AuthenticationTypes.Secure);
System.DirectoryServices.DirectorySearcher search = new System.DirectoryServices.DirectorySearcher(entry);

if (username.Contains("."))
{
string Fname = username.Substring(0, username.IndexOf("."));
Fname = Fname.Replace(" ", "");

string[] splitString = username.Split(new char[] { '.' });
username = splitString[splitString.Length - 1];

string email = Fname + "." + username;


search.Filter = "(mail=" + email + "*" + ")";

}


//--------If condition for Space and Dot search Filter---------------------------------------

if (username.IndexOf(" ") > 0)

search.Filter = "(cn=" + username + ")";


SearchResult result = search.FindOne();


if (result != null)
{

// user exists, cycle through LDAP fields (cn, telephonenumber etc.)

ResultPropertyCollection fields = result.Properties;

foreach (String ldapField in fields.PropertyNames)
{
// cycle through objects in each field e.g. group membership
// (for many fields there will only be one object such as name)
foreach (Object myCollection in fields[ldapField])
{
if (ldapField == "name")
employee.name = myCollection.ToString();
if (ldapField == "sn")
employee.surname = myCollection.ToString();
if (ldapField == "title")
employee.title = myCollection.ToString();
if (ldapField == "company")
employee.company = myCollection.ToString();
if (ldapField == "mobile")
employee.mobile = myCollection.ToString();
if (ldapField == "postalcode")
employee.postalcode = myCollection.ToString();
if (ldapField == "t
shan1395 24-Feb-11 22:55pm    
Hi Nishant ,

Embedded your code in to the existing Dll file but its throwing error

The type or namespace name'collection' could not be found
Nish Nishant 24-Feb-11 23:26pm    
You are missing a namespace.

using System.Collections.ObjectModel;
shan1395 24-Feb-11 23:58pm    
Thanks Nishant

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900