Tag Archives: C#

Atom-128 algorithm in C#

Encode method:

Decode method:

Usage:

Turn monitor on/off in C#

The following snippet will allow you to change your monitor’s state to either off/on or standby mode. Unlike other methods this one works on Windows 7 as well (tested under Windows 7 64bit).

The first step is to include in your class the following code:

This will allow us to send a WM_SYSCOMMAND message using SendMessage to alter the state of the monitor.

Finally, add the method which we will be calling when we want to change the monitor’s state:

Simply call the SetMonitorState method with the desirable state you want to change your monitor’s state to.

Usage:

Keep in mind that the SC_MONITORPOWER commands supports devices that have power-saving features, so depending on the monitor’s brand/drivers/firmware results might vary.

Check if user has administartor rights

In numerous cases it is essential to know if the user has administrator rights or not. A good example for this would be when we want to decide if we want to write a value in HKEY_LOCAL_MACHINE which requires administrator rights or HKEY_CURRENT_USER which doesn’t.

The following method will return true if the application is running under administration rights or false if its not.

Create shortcut programmatically in C#

In cases where we need to create a shortcut for our application or for any other reason, the Windows Script Host Object Model library allows us to do just that.

In order to access the classes that will enable us to create shortcuts we need to add the Windows Script Host Object Model library as a reference to our project first.

  1. Right click on your project
  2. Click “Add Reference…”
  3. Select the “COM” tab on the left
  4. Search for Windows Script Host Object Model and add it as a reference

Windows Script Host Object Model add reference

After you successfully add the reference in your project you should be able to use the snippet bellow to create shortcuts as you please.

Create shortcut:

Usage:

Start application at Windows startup with C#

The following snippet will allow you to add your application in the registry so it will launch when Windows start. Alternative you can use Environment.SpecialFolder.Startup to place a shortcut of your application in the startup folder which will have the same effect.

Note that this snippet will add an entry in HKEY_CURRENT_USER which means the program will only launch at startup for the user that is currently logged in when you run the code. If you want your program to run at startup for all users you will need to use HKEY_LOCAL_MACHINE instead but keep in mind that you will require administration rights in order to do that.

Register program to start with Windows:

Stop program from starting with Windows: