< Browse > Home /

| RSS

Convert Hexadecimal To Number In C#

To convert a Hexadecimal to a number we can use an overload of Convert.ToInt32 method.

Using the overload I can convert for example hex string “BB” to a number.
int number = Convert.ToInt32("BB", 16);

And the number variable gets assigned value of 187.

[ More ] March 24th, 2009 | No Comments | Posted in Code Snippets |

Get Name Of Current Executing Assembly In C#

This is where reflection comes in handy. The following code snippet shows you how to get the name of executing assembly.
string assemblyName;
assemblyName = System.Reflection.Assembly.GetExecutingAssembly().FullName;
Console.WriteLine(assemblyName);

 
Output for the above code returns the fully qualified name as shown below

 
To just get the Assembly name we can use this snippet.
string assemblyName;
assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
Console.WriteLine(assemblyName);

Here is the output.

[ More ] March 23rd, 2009 | No Comments | Posted in Code Snippets |

Get CD Or DVD Drive Information Using WMI And C#

This snippet shows you how to get information about all CD / DVD drives on your machine using WMI and C#. To run the code you need to add reference to System.Management.
ManagementObjectSearcher mgmtObjects =
new ManagementObjectSearcher("Select * from Win32_CDROMDrive");

foreach (var item in mgmtObjects.Get())
{
Console.WriteLine("Drive Letter - \t" + item["Drive"]);
Console.WriteLine("Name - [...]

[ More ] March 9th, 2009 | No Comments | Posted in Code Snippets |

Programmatically Take Screenshot Using C#

To do this you will need to add references to System.Drawing and System.Windows.Forms. Make sure that you have following using statements:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

 
In your method add this code. It will take a screenshot of your primary screen and save it to a file.
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
[...]

[ More ] February 26th, 2009 | 2 Comments | Posted in Code Snippets |

Get A List Of Running Processes Using C# - Code Snippet

This code will retrieve a list of currently running process on a machine and write their names to console.

// Get A List of process using
// System.Diagnostics.Process class
List<Process> processes = Process.GetProcesses().ToList();

// Write them out to Console
processes.ForEach(x => Console.WriteLine(x.ProcessName));

[ More ] January 27th, 2009 | No Comments | Posted in Code Snippets |

var Keyword In C#

I have a confession to make. When I saw var keyword for the first time I did not like it at all. My first opinion was that var was so variant like. Call me a control freak but I like to clearly see what I am declaring. And var was something I thought made the [...]

[ More ] January 6th, 2009 | 2 Comments | Posted in Programming |

Code Sample: Programmatically Download File Using C#

While browsing forums today I came across a question which asked for a solution to download a file from a web server programmatically. The solution is very simple and below is the code which achieves the goal. Here I am downloading a file asynchronously on Button Click.

1: private void buttonDownloadFile_Click(object sender, EventArgs [...]

[ More ] September 17th, 2008 | 5 Comments | Posted in Programming |

Fundamentals: Deep Cloning In C#

To make an object cloneable in .NET we should implement ICloneable interface. ICloneable is a simple interface with only one method Clone(). In this post I will show you how to make a deep clone of an object.

class Program
{
static void Main(string[] args)
{
[...]

[ More ] September 1st, 2008 | 2 Comments | Posted in Programming |

Get A List Of Installed Applications Using LINQ And C#

To get a list of installed applications we need to look into registry. Microsoft.Win32 namespace contains objects which can be used to work with Windows Registry. In this post I will show you some code where I use the power of LINQ to retrieve and display a list of all applications installed on a machine.
The [...]

[ More ] August 29th, 2008 | 10 Comments | Posted in Programming |

Programmatically Retrieve Information About Windows Services Using ServiceController

ServiceControl can be used to get information about a Windows Service on a machine.  In this post I will show you how to retrieve information about a service using ServiceControl.
To begin with lets examine the properties for ServiceControl. Through the designer we can set the ServiceName property. This can be set to the name of [...]

[ More ] August 29th, 2008 | No Comments | Posted in Programming |

Tutorial Reading A Text File Using LINQ

Introduction
At times us developers have to deal with delimited text files in our applications. Such files have been around since yonks and I often come across data import/export tasks where delimited files are used. Till now the common way in .NET has been to read each line and then extract data using some sort of [...]

[ More ] August 13th, 2008 | 6 Comments | Posted in Programming |

Write To Vista Event Log Using C#

Event Log is a central place to log application events. These events can be errors, warnings or just information. Each event log entry in Windows Vista has a level of event, date and time the event occurred, source of event, an event Id and a task category. While event logs such as Application, System, Security [...]

[ More ] August 7th, 2008 | 9 Comments | Posted in Programming |
  • Page 1 of 2
  • 1
  • 2
  • >