How to ignore the Alt-F4 key combination in C#
The Alt-F4 is a windows global shortcut which closes an active window. For some reason you may not be fond of this behavior. After doing some digging around, I finally found one method to ignore this request by overriding the ProcessDialogKey() method of Windows Form.
Here is the simple snippet which will ignore the Alt + F4 combination.
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == (Keys.Alt | Keys.F4))
{
return true;
}
return base.ProcessDialogKey(keyData);
}
Happy Programming.
How to programmatically start and stop windows services using C#
Today I got chance a to work around windows services. And I found ServiceController class, which helps to start, stop and get status on any windows service installed. Here is the code snippet which will get status of MySql service installed, and if it is not started, starts it. After two minutes again stops it.
const string serviceName = "MySql";
var serviceController = new ServiceController(serviceName);
if (serviceController.Status != ServiceControllerStatus.Running)
{
serviceController.Start();
serviceController.WaitForStatus(ServiceControllerStatus.Running);
}
Thread.Sleep(2 * 60 * 1000); //Sleeping
if (serviceController.Status != ServiceControllerStatus.Stopped)
{
serviceController.Stop();
serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
}
Note: This will not work, if your service is in disabled state. You will get an Invalid Operation exception, with message like this Cannot start service MySQL on computer ‘.’.
Happy Programming
How to read and write ini files using C#

The INI file format is an informal standard for configuration files for some platforms or software. INI files are simple text files with a basic structure composed of “sections” and “properties”. They are a deprecated standard on the Windows operating system. The use of the “INI file” has been changed in Windows in favor of the registry, and deprecated in .NET in favor of XML .config files. The name “INI file” comes from the filename extension usually used, “.INI”, that stands for “initialization”. If you are working with legacy applications you may require to read and manipulate ini files. Microsoft .Net framework doesn’t have API to manage ini files. But we can do it using WIN32 API calls. You can use WritePrivateProfileString() and GetPrivateProfileString() methods to write to ini file and read from ini file. Here I implemented a wrapper around these API functions, to read and write ini files.
public sealed class IniFile
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool WritePrivateProfileString
(string section, string key, string value, string fileName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern uint GetPrivateProfileString
(string section, string key, string defaultValue,
StringBuilder returnValue, uint size, string fileName);
private string _fileName;
public IniFile(string fileName)
{
_fileName = fileName;
}
public void Write(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, _fileName);
}
public string Read(string section, string key)
{
StringBuilder stringBuilder = new StringBuilder(255);
GetPrivateProfileString(section, key, string.Empty,
stringBuilder, 255, _fileName);
return stringBuilder.ToString();
}
}
And you can use IniFile class like this
var ini = new IniFile(@"C:\Samples\Helloworld.ini");
ini.Write("General", "Show Splash screen", "true");
MessageBox.Show(ini.Read("General", "Show Splash screen"));
Happy Programming
ASP.NET MVC 4 Beta released
A few days ago Microsoft released the ASP.NET MVC 4 Beta. This is a significant release that brings with it a bunch of great new features and capabilities. The ASP.NET MVC 4 Beta release works with VS 2010 and .NET 4.0, and is side-by-side compatible with prior releases of ASP.NET MVC (meaning you can safely install it and not worry about it impacting your existing apps built with earlier releases). It supports a “go-live” license that allows you to build and deploy production apps with it. Click here to download and install it today. The ASP.NET MVC 4 Beta will also be built-into the upcoming VS11 / .NET 4.5 beta that is coming out shortly. This week’s beta doesn’t work with the previous VS11 developer preview that shipped last September – if you are using the VS11 Developer Preview (or have it installed) you’ll want to wait until the VS 11 beta is released before trying out the new ASP.NET MVC 4 Beta functionality.
How to find deleted items in source control explorer
If you want to undelete deleted files using UI, you need to enable Source control explorer to display deleted files. This option can be enabled using Tools > Options > Source Control > Visual Studio Team Foundation server, and tick the Show deleted items in the Source control explorer checkbox.

Visual Studio Team Foundation server - Settings
This will display all the deleted files in Source control explorer with a small red symbol in the icon.
Happy programming