Category Archives: C#

How to know if a process has stopped or started using events in C#

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_ProcessStartTrace and 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.

In your constructor the event handlers need to be added.

and then their event methods that will be trigged when a process either starts or stops.

And finally we need to start the events by using

Remove digital signature from a file using C#

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.

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.

UPNP port forwarding – The easy way

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.

How to download a file in C# (progressbar and download speed)

This is a simple snippet that will allow you to download a file from the internet in C# while being able to display the download percentage, download speed and the amount of total data received while downloading.

This is the example form of what to expect when you have the code in place:

How to encrypt and decrypt files in C#

The following snippets will allow you to encrypt and decrypt files in C#.

Needless to say there are numerous methods that this can be achieved but for the reasons I will explain at the end I came about using this one in my projects.

Also keep in mind that assuming you want to implement something like this in your project you should extend the error catching to detect invalid key lengths, file locations and so on.