Finding registered application for an extension.

One day my colleague asked me if there is a way to know the registered application to a specific extension, or how OS executes Notepad when user double clicks on any txt files. Here is my finding over it. Read more »

Different ways to cut the current line(VS 2008)

In earlier days if I had to cut the current line from the visual studio I used to select all the line, keeping Cursor at the beginning and pressing Shift+End key to select the whole line and Cut the line using Ctrl+X, later I knew I could only use Ctrl+X without selecting anything from the line.Again later I knew there was  another way to do the same job; keeping cursor at the desired line and pressing Shift+Delete. Again later I found Ctrl+L would also perform the same job. So there are 4 ways I know choose whatever suits you best.

Copying Text From MessageBox

Ever wondered you could actually copy text from a MessageBox. I  found this great gem in one blog(sorry I forgot the name).

Press Ctrl+C while MessageBox has a focus.

This produces output as:

—————————

Error

—————————

Some Error Code: 0x0123456F

—————————

OK

—————————.

This really helps your frustrating minutes to write those Error code manually to a text editor.

Isn’t it a great tip?

LinQ!!Getting the associated file icons

LinQ has always fascinated me ever since it was first released and I have been learning it ever since. The following snippet could act as a tutorial, especially to the beginners thriving to learn LinQ. Before writing this, I did a bit of browsing on how to get the associated icon for the file extension. I found many likely hits using primitive techniques but none using LinQ. So I decided to write one myself. Even though the code isn’t optimized, it does what it is meant to do and more, it is quite simple to read and understand. Here goes:

var x = from A in

from B in

from C in

from D in

(from E in Registry.ClassesRoot.GetSubKeyNames()

where

E.StartsWith(“.”)

select

new

{   Extension = E,

Key =

Convert.ToString(Registry.ClassesRoot.OpenSubKey(E).GetValue(“”))

})

where D != null

select new {D.Extension, Ico = Registry.ClassesRoot.OpenSubKey(D.Key)}

where C.Ico != null

select new {C.Extension, Val = C.Ico.OpenSubKey(“DefaultIcon”)}

where B.Val != null

select new {B.Extension, FilePath = B.Val.GetValue(“”) as String}

where A != null && A.FilePath != null && A.FilePath.Split(‘,’).Length > 1

select

new

{

A.FilePath,

A.Extension,

Icon =

My.Utilities.IconExtractor.GetIcon(A.FilePath.Split(‘,’).ToArray()[0],

Convert.ToInt32(A.FilePath.Split(‘,’).ToArray()[1]))

};

And another utility class to extract icon from file.

namespace My.Utilities

{

using System;

using System.Runtime.InteropServices;

using System.Drawing;

public class IconExtractor

{

[DllImport("Kernel32.dll")]

static extern int GetModuleHandle(string lpModuleName);

[DllImport("Shell32.dll")]

static extern IntPtr ExtractIcon(int Hinst, string FileName, int IconIndex);

[DllImport("User32.dll")]

static extern int DestroyIcon(IntPtr hIcon);

public static Icon GetIcon(string fileName,int index)

{

IntPtr HIcon=IntPtr.Zero;

try

{

HIcon = ExtractIcon(GetModuleHandle(String.Empty), fileName, index);

if (HIcon == IntPtr.Zero) return null;

return Icon.FromHandle(HIcon).Clone() as Icon;

}

finally

{

DestroyIcon(HIcon);

}

}

}

}

RichTextBox without Image

One of my friend asked me if there is a way that could prevent Images being pasted in the RichTextBox control, so I did a little code that allows RichTextBox control not to include Images.
The solution to this problem is very simple just override the OnKeyDown event and check if the clipboard contains data of Bitmap Type.

public class RichTextBoxWithoutImage:RichTextBox

{

protected override void OnKeyDown(KeyEventArgs e)

{

base.OnKeyDown(e);

/*check for Ctrl+V*/

if (e.Control && e.KeyCode == Keys.V)

{

IDataObject obj = Clipboard.GetDataObject();

if (obj.GetDataPresent(typeof(Bitmap)))

{

e.Handled = true;

}

}

}

}

Getting information string from enum

The Idea came while I was writing a wrapper for PE file structure early this year that was even before I ever started writing bog. Loads of Enum has to be provided with their information. The solution was simple, attach a custom attribute to each member of the Enum and later retrieve them using reflection.

Here is how I did it all, Read more »

7-zip wrapper, Extracting password protected file

Recently there has been a 7-zip C# wrapper library in codeproject.com written by Eugene Sichkar. The author has done a tremendously great job he deserves a thank.

By default user can not simply use the native 7z.dll directly into the C# project this is because of the incomplete COM interfaces implementations in 7-Zip code. So his wrapper comes to a rescue but author says the wrapper is incomplete in fact it lacks everything (I don’t think he is true). What I found is  Currently wrapper doesn’t support extract feature for password protected files.I tweak his wrapper class to make it work for password protected files Read more »

Switch Builder

I had to write a switch case for an Enum that contains huge amount of members. Writing each case with hitting the keyword is frustrating and time consuming. So I wrote a little code that builds a switch case for every member of Enum in an Assembly.

The Logic behind the scene is extremely simple. What one need to do is.

  • load an assembly
  • Enumerate all Enum types from the loaded assembly.
  • Build the switch case

Load and Enumerat all Enums from an assembly and store it in a combo box Read more »

Glassy Control

I was fiddling with panel appearance as I wanted it to have a Glassy effect. I don’t know if I got it exactly what I wanted but the result somehow looks appealing. You can also play with it especially into Rectangles for better results (well, don’t know if it makes). Source

IComparer How to

Sometimes, when sorting real object life no longer remains easier as it is whilst making sort operation in simple data types. Consider a following example, you are about the sort cars from its collection. How do you do this? There is in fact no operator like <, >, == that meets your all requirements unless you code them explicitly.
As everyone has already known about ArrayList class, that contains a public method Sort(), Luckily user can also feed parameter about how sorting method will proceed.
Parameter I am talking about is IComparer interface that provides only one method.
The implementation is self-explanatory. Source

Follow

Get every new post delivered to your Inbox.