65.9K
CodeProject 正在变化。 阅读更多。
Home

Velde.Utilities - 用于 Active Directory、数据库、文件、网络等的 C# 工具类。

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.42/5 (9投票s)

2008年3月26日

CPOL
viewsIcon

57708

downloadIcon

953

用于 Active Directory、数据库、文件、网络等的 C# 工具类。

引言

这个工具类是为了简化与 Active Directory 和 SQL 相关的常见任务而编写的。此后,它已更新为包含文件操作、网络和基本日期。

背景

许多 Active Directory 函数都是从 Code Project 文章thund3rstruck 修改而来。

Using the Code

Velde.Utilities 包含几个类。每个类都可以拆分成独立的库。

这个类需要 Interop.ActiveDs.dll - 已包含。

Active Directory - Velde.Utilities.AD

ad.JPG

//
// Instantiate the AD Class
//

Velde.Utilities.AD AD = new Velde.Utilities.AD();

//if you wish to enable debugging
AD.debug = true; // this will cause any errors to be shown via MessageBox

// the constructor populates the domainDN field with the distinguishedName of the 
// domain to be used by LDAP in the rest of the functions. 

//What if we want to reset the password for Joe Smith, username: jsmith??
// we must do two things, get the user's distinguishedName, then reset the password.
string accountDN = AD.returnProperty("jsmith", "distinguishedName");

if(! AD.resetPasword(accountDN, "myNew$uperc001P@$$W0RD")
{
    MessageBox.Show("Password was not reset!!");
}   

//
//Create a user account for user Kevin Lee
//                    baseDN for user                        
AD.createUserAccount("cn=users,dc=test_domain,dc=local", 
//           UID     password     firstName  lastName
            "klee","!Password1", "Kevin", "Lee");     

//
//Force Kevin Lee to reset his password
//
AD.setProperty("klee","pwdLastSet", 0); //forced to reset password on next logon

//
//deny Kevin Lee the ability to change his password
//
string kevinAccountDN = AD.returnProperty("klee", "distinguishedName");
AD.denyChangePassword(kevinAccountDN);

数据库 - Velde.Utilities.Database.?

当前支持:Access、Excel、SQL、Visual FoxPro。

sql.JPG

//
// Instantiate the SQL Class
// (database classes: Access, Excel, SQL, VFoxPro)
Velde.Utilities.Database.SQL sql = new Velde.Utilities.Database.SQL();

//if you wish to enable debugging
sql.debug = true;

//setup server connection
sql.server = "name or ip of server"
sql.database = "name of database";
sql.username = "username";
sql.password = "password";

//get some information populated to the dataTable
sql.execQuery("SELECT * FROM table WHERE columnName LIKE 'pattern'");

//display the results
for(int i=0;i<sql.dt.Rows.Count;i++)
{
    Console.WriteLine(sql.dt.Rows[i]["columnName"].ToString());
}

//update the datatable
sql.dt.Rows[rowToModify]["columnName"] = "test";

//update the database to match the changes in the datatable
sql.updateTable(sql.da, sql.dt);

网络 - Velde.Utilities.Network

network.JPG

//
// Instantiate the Network Class
//
Velde.Utilities.Network net = new Velde.Utilities.Network;

//if you wish to enable debugging
net.debug= true;

//check to see if a host is on the network
if(net.isAlive("IP or DNS Name"))
{
    Console.WriteLine("Host is alive!");
}

//check to see if a host is listening on the smtp port
if(net.isAlive("IP", 25))
{
    Console.WriteLine("Server accepting connections.");
}

//send an email-  from, to, subject, body, bodyHTML, server, port
net.sendMail("me@mydomain.com","someone@theirDomain.com", "Subject", 
    "body", true/false, "serverName or IP", 25);

//shutdown remote host - requires admin on remote machine
if(net.killHost("IP or DNS name of host"))
{
    Console.WriteLine("Host accepted shutdown command.");
}

未显示的类

files.JPG time.JPG

摘要

希望这套类可以节省您的时间,或者让您更好地了解如何执行某些任务。如果您决定在您的任何项目中将此代码使用,或者您认为我的想法不切实际,请告诉我。

历史

  • 2008-3-26 - 上传文章
© . All rights reserved.