Click here to Skip to main content
15,885,909 members
Home / Discussions / C#
   

C#

 
AnswerRe: Please help............Unable to open "Field Explorer" of Crystal Report Pin
Verghese23-Jun-08 9:56
Verghese23-Jun-08 9:56 
GeneralRe: Please help............Unable to open "Field Explorer" of Crystal Report Pin
leppie23-Jun-08 11:21
leppie23-Jun-08 11:21 
QuestionVisual C# - Prevent console window from launching Pin
nyjcr23-Jun-08 7:49
nyjcr23-Jun-08 7:49 
AnswerRe: Visual C# - Prevent console window from launching Pin
Spacix One23-Jun-08 7:54
Spacix One23-Jun-08 7:54 
GeneralRe: Visual C# - Prevent console window from launching Pin
nyjcr23-Jun-08 8:05
nyjcr23-Jun-08 8:05 
Questionmargins Pin
netJP12L23-Jun-08 6:25
netJP12L23-Jun-08 6:25 
AnswerRe: margins Pin
Luc Pattyn23-Jun-08 6:56
sitebuilderLuc Pattyn23-Jun-08 6:56 
QuestionMatching properties in LDAP & AD Pin
mabby21623-Jun-08 5:29
mabby21623-Jun-08 5:29 
Hello Everyone...i'm creating a code that will scan LDAP and Active Directory for a fax number given to users and then it will compare both servers and will generate a text file with the discrepancies on both servers(only the numbers that don't match). The program seems to work but for some reason in the output i get a couple of users who have the same fax number in both servers. i wonder if i created the foreach loops correctly...can someone please guide me?????


namespace fax
{

// This sorted collection provides easy access to the FaxEntry objects added to it
// It also overrides ToString() to facilitate easy writing of results to the text file

public class FaxEntries : SortedList<string,>
{
System.Text.StringBuilder sb = new StringBuilder();

public override string ToString()
{
// If list is not already built up

if (sb.Length == 0)
{
// header
sb.AppendLine("XPID\t\tDisplay Name\tLDAP Fax#\tAD Fax#");
sb.AppendLine("-------------\t--------------\t-------------\t-------------");

// Iterate the collection and output each FaxEntry object via it's (overrided) ToString() method
foreach (KeyValuePair<string,> CurrItem in this)
sb.AppendLine(CurrItem.Value.ToString());
}

// Return the results
return sb.ToString();

}
}

public class FaxEntry
{
public string XPID;
public string DisplayName;
public string LDAPFaxNumber;
public string ADFaxNumber;

public FaxEntry(string XPID, string DisplayName, string LDAPFaxNumber, string ADFaxNumber)
{
this.XPID = XPID;
this.DisplayName = DisplayName;
this.LDAPFaxNumber = LDAPFaxNumber;
this.ADFaxNumber = ADFaxNumber;
}

// create an easily readible one line entry for the result
public override string ToString()
{
return Pad(XPID) + Pad(DisplayName) + Pad(LDAPFaxNumber) + Pad(ADFaxNumber);

}

// Add an appropriate number of tab characters to create a nice looking display to the passed string
string Pad(string ToPad)
{
if (string.IsNullOrEmpty(ToPad))

return "\t\t";

return ToPad + (ToPad.Length < 8 ? "\t" : string.Empty) + "\t";

}
}

class Program
{
static FaxEntries Entries;
static void Main(string[] args)
{
// The two properties to compare
string ComparisonProperty2 = "facsimiletelephonenumber";
string ComparisonProperty1 = "fax";

// The two servers to attach to LDAP/Unix and AD
string Server1 = @"LDAP://";
string Server2 = @"LDAP://";

Entries = new FaxEntries();

// Call compare to perform the comparison
Compare(Server1, ComparisonProperty1, Server2, ComparisonProperty2);

// Write the results to a file
try
{
System.IO.File.WriteAllText(@"C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\fax.txt", Entries.ToString());

// Write the results to the command line
Console.Write(Entries.ToString());

// Open a blank email
sendMyMail();

Console.ReadKey();
}

catch (Exception ex)
{
Console.WriteLine(ex.InnerException);

}
}

/// ////////////////////////////////////////////////////////////////////////

static void sendMyMail()
{
try
{
MailAddress from = new MailAddress("rillo@company.com");
MailAddress to = new MailAddress("mo@company.com");
MailMessage message = new MailMessage(from, to);

message.Body = "AD and LDAP lbpcsfax Discrepancies Report";
message.Body += Environment.NewLine;
message.Body += Environment.NewLine;

try
{

message.Attachments.Add(new Attachment(@"C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\fax.txt"));

}

catch (Exception e1)
{

message.Body += "Will not attach " + Entries + " because " + e1.Message;

}

message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "AD and LDAP fax Discrepancies Report";
message.Priority = MailPriority.Normal;
message.SubjectEncoding = System.Text.Encoding.UTF8;
SmtpClient client = new SmtpClient("mailhost.company.com");

try
{

client.Send(message);

}

catch (Exception exx)
{

Console.WriteLine("Error sending email " + exx.GetBaseException() + " " + exx.InnerException);
}

message.Dispose();
}

catch (Exception e)
{

Console.WriteLine("Error sending email " + e.GetBaseException() + " " + e.InnerException);
}
}

/// ////////////////////////////////////////////////////////////////////////////////////

static string GetPropertyForDisplay(SearchResult result, string Name)
{
if (result == null)

return string.Empty;

ResultPropertyValueCollection Items = result.Properties[Name];

if (Items != null)
{
if (Items.Count == 1)

return Name + " = " + Items[0].ToString();

else
{
string ret = Name + " = ";

foreach (object myKey in Items)

if (myKey != null)

ret += "\t" + myKey.ToString();

return ret;

}
}

return string.Empty;

}

static string GetProperty(SearchResult result, string Name)
{
if (result == null)

return string.Empty;

ResultPropertyValueCollection Items = result.Properties[Name];

if (Items != null)
{
if (Items.Count == 1)

return Items[0].ToString();

else
{
string ret = "";

foreach (object myKey in Items)

if (myKey != null)

ret += "\t" + myKey.ToString();

return ret;

}
}

return string.Empty;

}

static void Compare(string Server1, string PropertyToCompare1, string Server2, string PropertyToCompare2)
{

// Create the new FaxEntries Collection, one for each entry we find on either server property
Entries = new FaxEntries();

// Go get the matching LDAP users
SearchResultCollection server1Results = GetListByProperty(Server1, PropertyToCompare1, "uid");
SearchResultCollection server2Results = GetListByProperty(Server2, PropertyToCompare2, "sAMAccountName");


//added
if (server1Results != null)
{
foreach (SearchResult CurrItem in server1Results)
{
string sAMAccountName = GetProperty(CurrItem, "uid");

Entries.Add(sAMAccountName.ToLower(), new FaxEntry(sAMAccountName, GetProperty(CurrItem, "givenName"), GetProperty(CurrItem, PropertyToCompare1), null));

}
}

else
{
Console.WriteLine("server1Results returned nothing");

}

if (server2Results != null)
{
// Repeat on the second server with the second property and compare, adding as necessary

foreach (SearchResult CurrItem in server2Results)
{
string sAMAccountName = GetProperty(CurrItem, "sAMAccountName");

if (Entries.ContainsKey(sAMAccountName.ToLower()))
{
Entries[sAMAccountName.ToLower()].ADFaxNumber = GetProperty(CurrItem, PropertyToCompare2);
}

else
{
if (!Entries.ContainsKey(sAMAccountName.ToLower()))
{
Entries.Add(sAMAccountName.ToLower(), new FaxEntry(sAMAccountName, GetProperty(CurrItem, "givenName"), null, GetProperty(CurrItem, PropertyToCompare2)));

}
}
}
}

else
{
Console.WriteLine("server2 Results returned nothing");

}
}

static SearchResultCollection GetListByProperty(string LDAPPath, string PropertyToPerformComparisonAgainst, string userTag)
{
DirectoryEntry entry = new DirectoryEntry(LDAPPath);
DirectorySearcher usersearch = new DirectorySearcher(entry);

string myFilter = string.Empty;

entry.AuthenticationType = AuthenticationTypes.None;

if (userTag.ToLower().Equals("uid"))
{
myFilter = "(&(" + userTag + "=*)(" + PropertyToPerformComparisonAgainst + "=*)(objectClass=lborgperson))";

}

else
{
myFilter = "(&(" + userTag + "=*)(" + PropertyToPerformComparisonAgainst + "=*))";

}

Console.WriteLine(LDAPPath);
Console.WriteLine(myFilter);

try
{
usersearch.Filter = myFilter;

SearchResultCollection result = usersearch.FindAll();
Console.WriteLine(result.Count.ToString());

return result;
}

catch (Exception ex)
{
Console.WriteLine("Exception caught:" + ex.InnerException + " hmm " + ex.GetBaseException());

}

usersearch.Dispose();

entry.Close();

entry.Dispose();

return null;

}
}
}<
RantRe: Matching properties in LDAP & AD Pin
Spacix One23-Jun-08 7:38
Spacix One23-Jun-08 7:38 
QuestionNeed help on...............Crystal Report's Select Expert feature..............!!!! Pin
Verghese23-Jun-08 5:03
Verghese23-Jun-08 5:03 
AnswerRe: Need help on...............Crystal Report's Select Expert feature..............!!!! Pin
SomeGuyThatIsMe23-Jun-08 5:17
SomeGuyThatIsMe23-Jun-08 5:17 
GeneralRe: Need help on...............Crystal Report's Select Expert feature..............!!!! Pin
Verghese23-Jun-08 9:46
Verghese23-Jun-08 9:46 
GeneralRe: Need help on...............Crystal Report's Select Expert feature..............!!!! Pin
SomeGuyThatIsMe23-Jun-08 10:02
SomeGuyThatIsMe23-Jun-08 10:02 
QuestionRetrieve contextmenustrip item Pin
Hampus@foi23-Jun-08 4:59
Hampus@foi23-Jun-08 4:59 
AnswerRe: Retrieve contextmenustrip item Pin
Thomas Stockwell23-Jun-08 15:09
professionalThomas Stockwell23-Jun-08 15:09 
QuestionReading large quantity of data from a file Pin
LCI23-Jun-08 4:19
LCI23-Jun-08 4:19 
AnswerRe: Reading large quantity of data from a file Pin
Spacix One23-Jun-08 7:34
Spacix One23-Jun-08 7:34 
GeneralRe: Reading large quantity of data from a file Pin
LCI23-Jun-08 7:38
LCI23-Jun-08 7:38 
GeneralRe: Reading large quantity of data from a file Pin
Spacix One23-Jun-08 7:51
Spacix One23-Jun-08 7:51 
QuestionDisplay Jpeg and othe rtext on a windows forms app Pin
LCI23-Jun-08 4:09
LCI23-Jun-08 4:09 
AnswerRe: Display Jpeg and othe rtext on a windows forms app Pin
Jaime Olivares23-Jun-08 4:20
Jaime Olivares23-Jun-08 4:20 
GeneralRe: Display Jpeg and othe rtext on a windows forms app Pin
LCI23-Jun-08 4:23
LCI23-Jun-08 4:23 
GeneralRe: Display Jpeg and othe rtext on a windows forms app Pin
Jaime Olivares23-Jun-08 4:57
Jaime Olivares23-Jun-08 4:57 
AnswerRe: Display Jpeg and othe rtext on a windows forms app Pin
Christian Graus23-Jun-08 11:28
protectorChristian Graus23-Jun-08 11:28 
Question.NET Form Pin
_Tom_23-Jun-08 4:09
_Tom_23-Jun-08 4:09 

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.