Thursday, January 22, 2009

C#: Connect ActiveDirectory

To Connect the Active Directory, use the following method



using System;
using System.DirectoryServices;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Collections;

private System.DirectoryServices.DirectoryEntry AD = null;
private System.Boolean connected = false;

public System.Boolean Connect(string loginPath)
{
try
{
AD = new System.DirectoryServices.DirectoryEntry(loginPath);

connected = true;
}
catch (Exception e)
{
error = true;
connected = false;
errorLog.Append(e.ToString());
}
return connected;
}

C#: Active Directory Group Members

Hi,

If you pass the groupName to this method, it will return the list of all memebers available in this group.



using System;
using System.DirectoryServices;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Collections;

public System.Collections.ArrayList GetADGroupUsers(string groupName)
{
SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", groupName);
search.PropertiesToLoad.Add("member");
result = search.FindOne();

ArrayList userNames = new ArrayList();
if (result.Properties["member"] != null)
{

for (int counter = 0; counter <
result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];
string[] userdetail;
string delimStr = ",";
char [] delimiter = delimStr.ToCharArray();
userdetail=user.Split(delimiter,3);
user=userdetail[0].Remove(0,3);
userNames.Add(user);
}
}
return userNames;
}

C#: Get Folder Size

use Following method to get size of a folder,

To invoke this method, you need to pass directoryinfo object of the folder.



private double DirSize(DirectoryInfo folderdir)
{
double Size = 0;
FileInfo[] files;
//get all files for the current directory
if (folderdir.Exists)
{
files = folderdir.GetFiles("*.*");

//iterate through the directory and print the files
foreach (FileInfo file in files)
{
//get size of the files
Size += Math.Ceiling(file.Length / 1024.0); // size in KB ;
}
}
return Size;
}

C#: Delete File

To delete a file :



// Include the following namespace:

using System;
using System.IO;


// To delete a file, follow this format
File.Delete("c:\\file.txt");

VB: Write Cookie

This Code Explains, How to write cookie by using VB.NET



Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cookie As HttpCookie = New HttpCookie("UID")
cookie.Value = "myid"
cookie.Expires = #10/12/2005#
Response.Cookies.Add(cookie)
cookie = New HttpCookie("PASS")
cookie.Value = "mypass"
cookie.Expires = #10/12/2005#
Response.Cookies.Add(cookie)
End Sub

VB: Read Cookies

This Code Explains, How to read Cookies thru VB.NET Code



Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Clear()
Dim cookieCols As New HttpCookieCollection
cookieCols = Request.Cookies
Dim str As String
For Each str In cookieCols
ListBox1.Items.Add("Cookie: " + str)
ListBox1.Items.Add("Value:" & _
Request.Cookies(str).Value)
Next
End Sub

VB: Delete Cookies

Delete Cookies by using VB.NET


Dim cookieCols As New HttpCookieCollection
cookieCols = Request.Cookies
Request.Cookies.Remove("PASS")
Request.Cookies.Remove("UID")

C#: Delete Cookies

Delete all the cookies thru C#



void Page_Load()
{
string[] cookies = Request.Cookies.AllKeys;
foreach (string cookie in cookies)
{
BulletedList1.Items.Add("Deleting " + cookie);
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
}

VB: Manage multiple cookies

Manage multiple cookies Thru VB.NET



Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim coockieMultiplo As HttpCookie = New HttpCookie("cookieMultiplo")
coockieMultiplo.Values("nome") = "nome"
coockieMultiplo.Values("cognome") = "cognome"
coockieMultiplo.Values("telefono") = "telefono"
Response.Cookies.Add(coockieMultiplo)
ListBox1.Items.Clear()
Dim cookieCols As New HttpCookieCollection
cookieCols = Request.Cookies
Dim str As String
For Each str In cookieCols
ListBox1.Items.Add("Cookie: " + str)
ListBox1.Items.Add("Valore:" & _
Request.Cookies(str).Value)
Next
End Sub