Click here to Skip to main content
15,880,608 members
Articles / Database Development / SQL Server
Article

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

Rate me:
Please Sign up or sign in to vote.
4.42/5 (10 votes)
26 Mar 2008CPOL 56.3K   918   50   12
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
gino_montano26-Jun-12 6:57
gino_montano26-Jun-12 6:57 
Generalthanks Pin
mohmeh8311-Jul-09 20:27
mohmeh8311-Jul-09 20:27 
GeneralThanks Pin
sebbry12-Jun-09 0:38
sebbry12-Jun-09 0:38 
QuestionChange Password Pin
thanhchiho18-Nov-08 17:50
thanhchiho18-Nov-08 17:50 
GeneralresetPassword Pin
-Sean-1-Nov-08 16:28
-Sean-1-Nov-08 16:28 
GeneralRe: resetPassword Pin
-Sean-1-Nov-08 17:12
-Sean-1-Nov-08 17:12 
GeneralRe: resetPassword Pin
xExTxCx4-Nov-08 4:46
xExTxCx4-Nov-08 4:46 
Generalcool Pin
Kipetcoff18-Sep-08 21:31
Kipetcoff18-Sep-08 21:31 
GeneralWon't open in VS 2005 Pin
DABBee31-Mar-08 20:14
DABBee31-Mar-08 20:14 
GeneralRe: Won't open in VS 2005 Pin
xExTxCx1-Apr-08 5:27
xExTxCx1-Apr-08 5:27 
GeneralGreat work Pin
R%S26-Mar-08 20:34
R%S26-Mar-08 20:34 
GeneralRe: Great work Pin
xExTxCx27-Mar-08 5:45
xExTxCx27-Mar-08 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.