Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Velde.Utilities - C# Utility Class for Active Directory, Databases, Files, Networking etc.

4.42/5 (10 votes)
26 Mar 2008CPOL 2   922  
C# Utility class for Active Directory, databases, files, networking etc.

Introduction

This utility class was written to simplify common tasks relating initially to Active Directory and SQL. It has since been updated to include file manipulation, networking and basic dates.

Background

Many of the Active Directory functions were reworked from The Code Project article by thund3rstruck.

Using the Code

Velde.Utilities consists of several classes. Each could be split off into their own libraries.

This class requires Interop.ActiveDs.dll - included.

Active Directory - Velde.Utilities.AD

ad.JPG

C#
//
// 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);

Database - Velde.Utilities.Database.?

Currently Supported: Access, Excel, SQL, Visual FoxPro.

sql.JPG

C#
//
// 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);

Network - Velde.Utilities.Network

network.JPG

C#
//
// 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.");
}

Classes Not Shown

files.JPG time.JPG

Summary

Hopefully, this set of classes can save you some time, or give you a better idea of how to do certain tasks. Let me know if you decide to use this in any of your projects, or if you think that I'm way off base.

History

  • 3-26-2008 - Article uploaded

License

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