Tag Archives: C#
If you are looking for a way to set a global hotkey for your C# application that can be used without your form having focus I have created a decently commentated example for you below that you can use.
public partial class ExampleForm : Form
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
WinKey = 8
int id = 0; // The id of the hotkey.
RegisterHotKey(this.Handle, id, (int)KeyModifier.Shift, Keys.A.GetHashCode()); // Register Shift + A as global hotkey.
protected override void WndProc(ref Message m)
if (m.Msg == 0x0312)
/* Note that the three lines below are not needed if you only want to register one hotkey.
* The below lines are useful in case you want to register multiple keys, which you can use a switch with the id as argument, or if you want to know which key/modifier was pressed for some particular reason. */
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); // The key of the hotkey that was pressed.
KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF); // The modifier of the hotkey that was pressed.
int id = m.WParam.ToInt32(); // The id of the hotkey that was pressed.
MessageBox.Show("Hotkey has been pressed!");
// do something
private void ExampleForm_FormClosing(object sender, FormClosingEventArgs e)
UnregisterHotKey(this.Handle, 0); // Unregister hotkey with id 0 before closing the form. You might want to call this more than once with different id values if you are planning to register more than one hotkey.
There are numerous of ways to detect if a new process has started or stopped, sadly the majority of them are extremely inefficient as it requires you to keep looping through the active process constantly to see if a new one appeared in the array or if one is not there any more.
Luckily the windows
Win32_ProcessStopTrace classes are here to help out.
The first thing we need to do is reference System.Management.dll in our project. Then we need to define the scope in your class which we will be using.
After that we need to initialise the class which will contain the process start and process stopped events and add the handlers and their methods.
Add the two following variables in your Class.
ManagementEventWatcher processStartEvent = new ManagementEventWatcher("SELECT * FROM Win32_ProcessStartTrace");
ManagementEventWatcher processStopEvent = new ManagementEventWatcher("SELECT * FROM Win32_ProcessStopTrace");
In your constructor the event handlers need to be added.
processStartEvent.EventArrived += new EventArrivedEventHandler(processStartEvent_EventArrived);
processStopEvent.EventArrived += new EventArrivedEventHandler(processStopEvent_EventArrived);
and then their event methods that will be trigged when a process either starts or stops.
void processStartEvent_EventArrived(object sender, EventArrivedEventArgs e)
// A new process has started
void processStopEvent_EventArrived(object sender, EventArrivedEventArgs e)
// A process has been stopped
And finally we need to start the events by using
This small tool was created to simplify the process that was discussed at Remove digital signature from a file using C# and simply aid the people that didn’t have the knowledge on how to compile the code posted there.
This is a small standalone tool that will simply remove the digital signature of any file.
The usage is fairly simple. Either drag and drop the files you want to unsign on the application or use command prompt with the following parameters:
FileUnsigner.exe <options> <file1> <file2> …
/f Forces the program to remove a digital signiture even if one is not detected.
There are two reasons why you might want to consider removing the digital signature from some of the files you are using, especially third party libraries.
The first reason is because in many cases it will greatly speed up your programs start-up time. The reason for that is because if a file is signed with Microsoft digital certificate as an example on runtime it will attempt to verify the signature which in most cases it requires an internet connection. In cases where the user might not have an active internet connection or if the verification attempt is blocked by a firewall or any other reason, it will greatly increase the time your application will take to start.
Another important reason for removing a digital signature from a library you want to distribute alongside with your software is that a lot of users feel alarmed when an application tries to establish a connection at start-up, especially if the type of the application you are making doesn’t sound like the type it requires to connect somewhere.
Thankfully it is fairly easy to remove a signature from a file using the
ImageRemoveCertificate API function. Below you can find a snippet that illustrates how it can be implemented.
private static extern bool ImageRemoveCertificate(IntPtr handle, int index);
private void UnsignFile(string file)
using (System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))
If you would like to use the above code but have no knowladge on how to compile it then simply download the already compiled version from FileUnsigner v1.0.
From my experience I noticed that the NATUPnP library is pretty unstable when it comes down to UPNP (mostly refusing to work on some routers regarding if the router is UPNP enabled or not) I decided to make a simple walk-through to help people resolve these kind of issues and aid them in creating a program that manual port forwarding is not needed, at least for UPNP enabled routers.
So, let’s get started. First you need to download the Mono.Nat library which can be found here. When the download is complete extract the contents of the .zip file anywhere you like.
In order to be able to take advantage of the library we need to locate the file we have extracted and add it as a reference to our project then import it in your project using
Now it’s time for some coding.