Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / XML

XML Querying, Text Streaming, Debug Capture, and LDAP

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
17 Jan 20062 min read 26.7K   240   18  
Library containing functions for XML Querying, Text Streaming, Debug Capture and LDAP.
using System;
using System.Web;
using System.Security;
using System.Data;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Diagnostics;
using System.DirectoryServices;
using System.ServiceProcess;
using System.IO;
using System.Windows.Forms;
using System.Collections;
using System.Threading;

using System.Net;
using System.Net.Sockets;
using System.Text;

namespace WernerReyneke.GPCL
{
	#region LDAPPlus_Class
	public class LDAPPlus
		/// <summary>
		/// Custom LDAP class for common LDAP (Lightweight Directory Access Protocol)(Active Directory) querying and authentication
		/// related operations.
		/// </summary>
	{
		private AuthenticationTypes atAuthentType;
		public LDAPPlus()
		{
		}
		//returns true if user (name and password) exists in Active Directory, false if not.
		public string Authenticate_Against_LDAP(string LDAPServerName, string UserName, string Password)
		{
			atAuthentType = AuthenticationTypes.Secure;
			System.DirectoryServices.DirectoryEntry deDirEntry = new DirectoryEntry("LDAP://" + LDAPServerName.ToString(), UserName.ToString(), Password.ToString(),atAuthentType); 
			try
			{                 
				string a = "Welcome to '" + deDirEntry.Name + "'";
				return a;
			}
			catch (Exception)
			{
				return "Access Denied! Or Active Directory for Network: " + LDAPServerName.ToString() + " does not exist";
			}
		}
		
	}
	#endregion LDAPPlus_Class

	#region StreamPlus_Class
	public class StreamPlus
		/// <summary>
		/// 
		/// </summary>
	{
		public StreamPlus()
		{
		}

		//returns a string with entire file contents
		public string ReadFromFile(string filename)
		{
			string contents = "";
			try
			{
				System.IO.StreamReader file = new System.IO.StreamReader(filename);
				contents = file.ReadToEnd();
				file.Close();
			}
			catch{}

			return contents.ToString();
		}

		//Returns a dynamically sized string type ArrayList object with entire file contents.
		public ArrayList ReadFromFileIntoArrayList(string filename)
		{
			string contentstemp = "";
			ArrayList contents = new ArrayList();
			try
			{
				System.IO.StreamReader file = new System.IO.StreamReader(filename);
				
				while((contentstemp = file.ReadLine()) != null)
				{
					contents.Add(contentstemp.ToString());
				}
				file.Close();
			}
			catch{}

			return contents;
		}

		//Appends the supplied text to a file.
		public int AppendToFile(string filename, string contents)
		{
			try
			{
				System.IO.StreamWriter file = new System.IO.StreamWriter(filename,true);
				
				file.WriteLine(contents.ToString());
				file.Close();
			}
			catch{}
			return 1;
		}

		//Overwrites/writes the supplied text file.
		public int WriteFile(string filename, string contents)
		{
			try
			{
				System.IO.StreamWriter file = new System.IO.StreamWriter(filename,false);
				file.WriteLine(contents.ToString());
				file.Close();
			}
			catch{}
			return 1;
		}

		//Deletes the supplied text file.
		public int DeleteFile(string filename)
		{
			try
			{
				File.Delete(filename.ToString());
			}
			catch{}
			return 1;
		}

		public bool CopyFile(string old_file_path, string new_file_path)
		{
			bool success = false;
			string path = old_file_path;
			string path2 = new_file_path;

			try 
			{
				// Create the file and clean up handles.
				//using (FileStream fs = File.Create(path)) {}

				// Ensure that the target does not exist.
				File.Delete(path2);

				// Copy the file.
				File.Copy(path, path2, true);
				//Console.WriteLine("{0} copied to {1}", path, path2);

				success = true;
			} 

			catch 
			{
				success = false;
			}
			return success;

		}
		
	}
	#endregion StreamPlus_Class

	#region DebugPlus_Class
	public class DebugPlus
	{
		public DebugPlus()
		{
		}

		//Sends the passed message as well as the originating application name and code-member inside application to the Debug listener.
		public static void SendToDebug(string message)
		{	
			string an = Thread.GetDomain().FriendlyName.ToString();
			StackTrace st = new StackTrace(false);
			string caller = st.GetFrame(1).GetMethod().Name;
			Debug.WriteLine("Application: " + an.ToString() + ", Function: " + caller);
			Debug.WriteLine("Message: " + message);
		}
		
	}
	#endregion DebugPlus_Class

	#region DataPlus_Class

	public class DataPlus
	{
		public DataPlus()
		{

		}
	

		//Returns a datarow[], resulting from a queried XML file:
		public System.Data.DataRow[] QueryXML(string str_xmlfilename, string where_criteria, string sorting, string primary_key)
		{
			System.Data.DataSet ds = new DataSet();
			ds.ReadXml(str_xmlfilename.ToString());
			System.Data.DataRow[] foundRows = null;
			DataRow[] rows = null;

			string strExpr;
			string strSort;
			System.Data.DataTable aTable = new DataTable();
			aTable = ds.Tables[0];

			strExpr = where_criteria.ToString();
			strSort = sorting.ToString();
			//all possible variations of sort and criteria strings:
			if(strExpr.ToString()=="*" && strSort.ToString()!="*")
			{
					
				foundRows = aTable.Select(primary_key.ToString() + " IS NOT NULL", strSort, DataViewRowState.CurrentRows);

			}
			if(strExpr.ToString()=="*" && strSort.ToString()=="*")
			{
				foundRows = aTable.Select(primary_key.ToString() + " IS NOT NULL", primary_key.ToString() + " ASC", DataViewRowState.CurrentRows);
			}
			if(strExpr.ToString()!="*" && strSort.ToString()=="*")
			{
				foundRows = aTable.Select(strExpr.ToString(), primary_key.ToString() + " ASC", DataViewRowState.CurrentRows);
			}
			if(strExpr.ToString()!="*" && strSort.ToString()!="*")
			{
				foundRows = aTable.Select(strExpr.ToString(), strSort, DataViewRowState.CurrentRows);
			}
			//end of all possible variations of sort and criteria strings:
				
			if( foundRows.Length <= 0 )
			{
				return rows;
			}
			else
			{
				return foundRows;
			}
		}
	}

	#endregion DataPlus_Class
}
	

	

	

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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