Home / Archive by category 'Code Snippets'

| RSS

Get Current Windows User In C#

This snippet will show you how to get the currently logged in user on windows.
System.Security.Principal.WindowsIdentity identity =
System.Security.Principal.WindowsIdentity.GetCurrent();

Console.WriteLine(identity.Name);

[ More ] May 12th, 2009 | No Comments | Posted in Code Snippets |

Get Width And Height Of Image In C#

To get width and height of an image we can use System.Drawing.Image class. This code snippet shows how to retrieve width and height of an image.

string filePath = @"c:\deepak\MeeGo.jpg";
Image img = Image.FromFile(filePath);
 
Console.WriteLine(string.Format("Height: {0}",img.Size.Height));
 
Console.WriteLine(string.Format("Width: {0}",img.Size.Width));

[ More ] March 31st, 2009 | 5 Comments | Posted in Code Snippets |

Get Windows Registry Size With WMI And C#

Windows Registry Size can be retrieved using WMI objects. This code snippet shows you how to get current size and maximum size for Windows registry.
ManagementObjectSearcher mgmtObjects =
new ManagementObjectSearcher("Select * from Win32_Registry");

foreach (var item in mgmtObjects.Get())
{
Console.WriteLine(string.Format("Current Size: {0}MB", item["CurrentSize"]));
Console.WriteLine(string.Format("Maximum Size: {0}MB", item["MaximumSize"]));
}

 
The output.

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

Reverse Array Elements Using C#

In .Net Framework reversing array elements can be done by using Reverse method on Array type. This code snippet shows you how this method can be used to reverse an array.
// Declare an array with 10 elements
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Output [...]

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

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 Free Disk Space Using T-SQL

To get free disk space for all physical drives on a machine we can use xp_fixeddrives extended stored procedure. An interesting this about this procedure is that it is not documented in books online.
EXEC xp_fixeddrives

Here is output on my machine.

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

SQL Server 2008 – Get All Indexes In A Database

You’ve got to love sys views in SQL Server. While learning about performance tuning on SQL Server 2008 I wanted to get a list of all Indexes on my database and the answer was as simple as it can be. Here i a statement which can be used to get a list of all indexes [...]

[ 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 | 1 Comment | 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 | 1 Comment | Posted in Code Snippets |

Get Last Row From Table Using LINQ To SQL

This query returns the last row from Sales.Customer table in Adventure Works database. I am using LINQPad to write and execute my query.
(from c in Customers
select c)
.OrderByDescending(x=> x.CustomerID).First()
 
Here is the result as displayed in LINQPad
 

[ 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 | 7 Comments | Posted in Code Snippets |

T-SQL Return Empty String Instead Of Null

There are times when we want to return empty strings for column(s) which have NULL values. T-SQL makes this easy with ISNULL function. Two parameters for this function are:

The column in question
Replacement value

 
Here is an example query for Customers table in Northwind database. This query will select Region column [...]

[ More ] February 11th, 2009 | No Comments | Posted in Code Snippets |
  • Page 1 of 2
  • 1
  • 2
  • >