Tag Archives: winforms
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
1 | using Mono.Nat; |
Now it’s time for some coding.
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.
Posted in C#.
Tagged aes, C#, cryptography, csharp, decrypt file, encrypt file, RijndaelManaged, snippet, winforms
List all Windows services in C#
I decided to make a tutorial to help the ones that were interested to learn how to go over the Windows services so they can either start/stop/pause them or just display them in a control. The code I’ll be showing is from a program I created with the purpose of listing all the services info in a listview control.
The first steps you will need to take is reference “System.ServiceProcess” which will allow us to use the ServiceController class and also add the following code on the top of our Form/Class:
1 2 | using System.ServiceProcess; using Microsoft.Win32; |
We start by declaring our ServiceController class and populating it with all the services that currently run in the system using .GetServices();
1 | ServiceController[] services = ServiceController.GetServices(); |
Then we need to use a foreach loop in order to go through all the services. For the sake of this tutorial I’ll be using only one try/catch, usually you will want more to display more accurate info based on what info you fail to retrieve, in this case if an error occurs the whole service won’t be added to the list.
1 2 3 4 5 6 7 8 9 10 11 12 | foreach (ServiceController service in services) { try { // Our code will go here } catch (Exception ex) { Console.WriteLine(ex.Message); } } |
It’s time to start populating our ListView columns. The columns we will be using is the services name/status/location/description. To do that we need to first declare a new ListViewItem that we will be using to add the information. The service name and status info are easy to obtained using the ServiceController class as follows:
Posted in C#.
Tagged C#, csharp, ServiceController, windows services, winforms
List Windows active processes in C#
Since some people were interested on how to get all the Window processes, I decided to make this tutorial. How you tailor it to your needs is up to you.
We start off by referencing System.Management
and adding the following on the top of our Form/Class:
1 2 | using System.Management; using System.Diagnostics; |
Then we need to declare our main classes. In this case that would be ManagementClass
and ManagementObjectCollection
in order to help us loop through the active process. Note that this can also be done by Process.GetProcesses()
but there is a limitation. Process.GetProcesses()
can only see the same bit process as your .exe files. So using that mean we wont be able to see all the processes, hence why we are using the two classes I mentioned above instead.
1 2 | ManagementClass management = new ManagementClass("Win32_Process"); ManagementObjectCollection mCollection = management.GetInstances(); |
After that we need to create our foreach loop which will help us get the information for each process we find and also a new listviewitem which we will be adding to our listview at the end of the loop so we can see the results.
1 2 3 4 | foreach (ManagementObject process in mCollection) { ListViewItem newListViewItem = new ListViewItem(); } |
For the sake of this tutorial we will only be concentrating in the process id/name/location and finally the description.
Posted in C#.
Tagged C#, csharp, ManagementClass, ManagementObjectCollection, processes, snippet, tutorial, Win32_Process, winforms