> ## Content Index
> Fetch the complete content index at: https://kudithipudi.org/llms.txt
> Use this file to discover other available public pages before exploring further.

# Programming : C#
- URL: https://kudithipudi.org/programming-c/
- Published: 2006-10-20T21:33:57.000Z
- Updated: 2006-10-20T21:33:57.000Z
- Description: I have been itching to program in C# for a long time now.. finally got a chance to try it out due to requirement at work....
- Author: admin
- Tags: Programming, Technology, Uncategorized, Windows, #wp, #wp-post, #Import 2026-08-23 02:32

I have been itching to program in C# for a long time now.. finally got a chance to try it out due to requirement at work. Here’s a small snippet of code that does a bunch of things  
1) Opens a directory and get a list of files in it  
2) Parses the file name into multiple variables  
3) Connects to Active Directory and gets properties for a user  
4) Creates an error file with list of errors.  
5) Creat an output file in csv format.

For the programming gurus out there, this should be pretty easy :).. I am sure that there are tons of errors in the way, I wrote the code, but this is a beginning :).

---

using System;  
using System.Collections.Generic;  
using System.Text;  
using System.IO;  
using System.DirectoryServices;

namespace sortAssetData  
{  
class Program  
{  
static void Main(string\[\] args)  
{  
// Create a Log file and output file

System.IO.TextWriter infoLog = new System.IO.StreamWriter(“sortAssetDataLog.txt”);  
System.IO.TextWriter outputLog = new System.IO.StreamWriter(“finalAssetData.csv”);

// Write the column headers into the output file  
outputLog.WriteLine(“Last\_Name, First\_Name, Display\_Name, Batt\_Check\_Version, Model\_Type, Model\_Number, Serial\_Number, Batt\_Manufacturer, Batt\_Model, Batt\_Serial\_Number, Affected”);

// Define the folder where the data files are sitting  
string fixDirectoy = “P:\\\\BatteryCheck\_Results”;

// Change working directory to the folder with all the files  
System.IO.Directory.SetCurrentDirectory(fixDirectoy);

// Create a directory object to get list of asset files  
System.IO.DirectoryInfo tempDirObject = new System.IO.DirectoryInfo(fixDirectoy);

// Get list of \*.csv files in the asset folder  
System.IO.FileInfo\[\] listOfFiles = tempDirObject.GetFiles(“\*.csv”);

// Process each file in the directory  
foreach (System.IO.FileInfo tempFile in listOfFiles)  
{  
// Display the current file that is being processed  
System.Console.WriteLine(“Processing : {0}”, tempFile.Name);  
infoLog.WriteLine(“Processing : {0}”, tempFile.Name);  
infoLog.Flush();

// Split the file name into Domain, Hostname, Username  
string tempData = tempFile.Name;  
// Removing the .csv at the end of the file name  
tempData = tempData.Replace(“.csv”, “”);  
string\[\] sortData = tempData.Split(new Char\[\] { ‘\_’ });

// If file name doesn’t contain 3 records (Domain, ComputerName and UserName), we ignore it  
if (sortData.Length != 3)  
{  
infoLog.WriteLine(“ERROR : Bad file format : {0}”, tempFile.Name);  
infoLog.Flush();  
continue;  
}

// Display the userdata we got from the file name  
System.Console.WriteLine(“\\nDomain :\\t {0}”, sortData\[0\]);  
System.Console.WriteLine(“Computer Name :\\t {0}”, sortData\[1\]);  
System.Console.WriteLine(“User Name :\\t {0}\\n”, sortData\[2\]);

// Get the users full name from ActiveDirectory  
System.DirectoryServices.DirectorySearcher searchUser = new System.DirectoryServices.DirectorySearcher();  
searchUser.Filter = String.Format(“(SAMAccountName={0})”, sortData\[2\]);  
searchUser.PropertiesToLoad.Add(“cn”);  
searchUser.PropertiesToLoad.Add(“givenname”);  
searchUser.PropertiesToLoad.Add(“sn”);  
System.DirectoryServices.SearchResult resultFromAD = searchUser.FindOne();

// If the username doesn’t have a AD account, note and skip to the next file  
if (resultFromAD == null)  
{  
System.Console.WriteLine(“ERROR : Bad User name : {0}”, sortData\[2\]);  
infoLog.WriteLine(“ERROR : Bad User name : {0}”, sortData\[2\]);  
infoLog.Flush();  
continue;  
}  
else  
{  
//Parse the file and write data to the log file and also to the screen  
System.IO.StreamReader tempSoureFile = new System.IO.StreamReader(tempFile.Name);  
// Read the first line in the file and ignore it  
string tempAssetData = tempSoureFile.ReadLine();  
// Read the second line and store it to write to outputfile  
tempAssetData = tempSoureFile.ReadLine();

string tempUserFirstName = resultFromAD.Properties\[“sn”\]\[0\].ToString();  
string tempUserLastName = resultFromAD.Properties\[“givenname”\]\[0\].ToString();  
string tempUserDisplayName = resultFromAD.Properties\[“cn”\]\[0\].ToString();  
outputLog.WriteLine(“{0},{1},{2},{3}”, tempUserFirstName, tempUserLastName, tempUserDisplayName, tempAssetData);  
outputLog.Flush();  
System.Console.WriteLine(“User First Name:\\t {0}”, tempUserFirstName);  
System.Console.WriteLine(“User Last Name:\\t\\t {0}”, tempUserLastName);  
System.Console.WriteLine(“Display Name:\\t\\t {0}”, tempUserDisplayName);  
}  
}  
System.Console.ReadKey();  
infoLog.Close();  
outputLog.Close();  
}  
}  
}

---