将控制台应用程序转换为类库 [英] Convert Console App to Class Lib

查看:90
本文介绍了将控制台应用程序转换为类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个代码可以检索所有活动的用户,但这是一个控制台应用程序.

我想将这个应用程序转换为dll并在我单击Windows应用程序中的按钮时调用该dll.

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.

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();
    }
}

推荐答案

创建一个新的DLL项目.向其添加一个新类,并添加一个返回IEnumerable<string>的静态方法.或者让它触发一个将字符串作为arg的事件-这样,您不必等待整个搜索完成就可以开始处理结果.

这是以前的方法:

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:

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:

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 });
        }
    }
}



SearchEventArgs 所在的位置:



where SearchEventArgs is:

public class SearchEventArgs : EventArgs
{
    public object Value { get; set; }
}


这篇关于将控制台应用程序转换为类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆