Singleton :-Singleton is controversial design pattern of OOPS. It allows you to restrict the number of instances of an object.It's an interface that allows a class to enforce that it is only allocated once. Sample Code :-Namespace :-using system.text;using System.collections;class SampleProgram{ public static void Main() { SingletonStructure...
How to read XML Node using C#
Load an XML FileXmlDocument xdXml = new XmlDocument();xdXml.Load("Index.xml"); Make a nodelist XmlNodeList xnNodes = xdXml.SelectNodes("/Tools/Download"); Walk through the list foreach (XmlNode node in xnNodes) { if (node.FirstChild.InnerText == ddlTools.Text) { //Get all the child nodes XmlNodeList childNodes = node.ChildNodes;...
Transparent images not displaying correctly
If images are not displaying correctly or showing a black spot at the back side of the image then play with the color depth property of imagelist or picture box.Example :- ImageList imageList = new ImageList();imagelist.colorDepth = colorDepth.32B...
Collection of Regular Expressions
Regular expression to match email address1 :- [\w-]+@([\w-]+\.)+[\w-]+2 :- (?(?![ ])(\w|[.])*@(\w|[.])*)3 :- ^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}$4 :- /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@ ([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/iRegular Expression to validate US-Phone number.Example :- (999) 999-9999 or (999)123-78691 :- /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/Regular...
Detect the Encoding Type of given file
Encoding type : To add new Encoding we have to add new member in here,with the CodePage value as an value of the new encoding and have to modify FileHandler.DetectEncoding function.public enum EncodingType{ WindowsANSI = 1252, Unicode = 1200, UnixANSI = 28591}You can pass the path of the file and this method returns the encoding...
Sorted List Implementation
In C# You can implement Sortedlist and sort the values according to the key.using System;using System.Collections;Code:static void Main(string[] args){ values in sorted list are sorted according to key SortedList myList = new SortedList(); myList.Add(12, "December"); myList.Add(9, "September"); myList.Add(10, "October"); ...
Hash Table Implementation
using System;using System.Collections; Code:static void Main(string[] args){ Create an object of hash table Hashtable myHash = new Hashtable(); Add key-pair values myHash.Add("Saurabh", 1); myHash.Add("Sandy", 2); myHash.Add("Vivek", 3); Move through hashtable data in cycle foreach (DictionaryEntry item in myHash)...
Get System Boot Configuration using C#
using System;using System.Management; Code:static void Main(string[] args){ WqlObjectQuery query = new WqlObjectQuery("SELECT * FROM Win32_BootConfiguration"); ManagementObjectSearcher find = new ManagementObjectSearcher(query); foreach (ManagementObject mo in find.Get()) { Console.WriteLine("Boot directory with files...
Get Environment variables..
Namespaces:using System;using System.Management;Code:static void Main(string[] args){ WqlObjectQuery query = new WqlObjectQuery("Select * from Win32_Environment"); ManagementObjectSearcher find = new ManagementObjectSearcher(query); Console.WriteLine("Description - Name - User Name - Value"); foreach (ManagementObject mo...
Obtain IP address and host
Namespaces:using System;using System.Net; Code:static void Main(string[] args){ Get Host Name string host = Dns.GetHostName(); Console.WriteLine("Hostname is: {0}", host); GetIP Entry IPHostEntry entry = Dns.GetHostByName(host); foreach (IPAddress ip in entry.AddressList) { Console.WriteLine("IP...
Send mail message to .net Environment (Enjoy Mail System)
Namespaces:using System;using System.Web.Mail; Code:static void Main(string[] args){ MailMessage mailMsg = new MailMessage(); mailMsg.From = "saurabhsingh_jnu06@yahoo.co.in"; mailMsg.To = "saurabhjnumca@gmail.com"; mailMsg.Cc = string.Empty; mailMsg.Bcc = string.Empty; mailMsg.Subject = "Here goes a subject"; mailMsg.Body...
Network Operations in C# (Play with system)
Retrieve DNS computer namepublic static void Main(string[] args){ Console.WriteLine(“DNS: {0}”, System.Net.Dns.GetHostByName“LocalHost”).HostName);}Retrieve NetBIOS computer namepublic static void Main(string[] args){ Console.WriteLine(“NetBIOS: {0}”, System.Environment.MachineName...
To get the system Icons(handle of system imagelist)
[StructLayout(LayoutKind.Sequential)] public struct SHFILEINFO { public IntPtr hIcon; public IntPtr iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplayName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] ...
To close the application from task manager.
If you have closed your appplication but still it runs in task manager then you can use this method to kill the thread.It happens only when some thread of your application is not closed.Code:-public const int SC_CLOSE = 0xF060; public const int WM_SYSCOMMAND = 0x0112; //Override method when You will close the windowprotected override void WndProc(ref...
Play with Date-Time-Formats in C#
Code:public static void Main(string[] args){ //Get the cureent date time DateTime datetTime= DateTime.Now; Formatting DateTime to full pattern (dddd, MMMM dd, yyyy hh:mm:ss) Console.WriteLine(dt.ToString("F")); Formatting DateTime to short date- time pattern (dddd, MMMM dd, yyyy, hh:mm) Console.WriteLine(dt.ToString("f")); ...
How to get Memory Info
Namespaces:using System;using System.Management; Code:static void Main(string[] args){ WqlObjectQuery query = new WqlObjectQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Memory"); ManagementObjectSearcher find = new ManagementObjectSearcher(query); //Traverse each management object. foreach (ManagementObject mo in find.Get())...
Open Control Panel Items using Shell (COM)
Namespaces:using System;// this is COM component that can be found under the name "Microsoft Shell Controls And Automation"// this must be added to project referencesusing Shell32;Code:static void Main(){ //Creating an object of shell to access all the control panel items. Shell shell = new Shell(); // accessibility options shell.ControlPanelItem("access.cpl");...
Get folder items using Windows folder dialog
Namespaces: using System; using Shell32; Code:static void Main(){ Shell shell = new Shell(); // open dialog for desktop folder // use appropriate constant for folder type ShellSpecialFolderConstants Folder folder = shell.BrowseForFolder(0, "folderPath/FilePath",0 ,ShellSpecialFolderConstants.ssfDESKTOP);...