Tag Archives: C#
Atom-128 algorithm in C#
Encode method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public static string Atom128Encode(string input) { string key = "/128GhIoPQROSTeUbADfgHijKLM+n0pFWXY456xyzB7=39VaqrstJklmNuZvwcdEC"; StringBuilder result = new StringBuilder(); int i = 0; int[] indexes = new int[4]; int[] chars = new int[3]; do { chars[0] = i + 1 > input.Length ? 0 : (int)input[i++]; chars[1] = i + 2 > input.Length ? 0 : (int)input[i++]; chars[2] = i + 3 > input.Length ? 0 : (int)input[i++]; indexes[0] = chars[0] >> 2; indexes[1] = ((chars[0] & 3) << 4) | (chars[1] >> 4); indexes[2] = ((chars[1] & 15) << 2) | (chars[2] >> 6); indexes[3] = chars[2] & 63; if ((char)chars[1] == 0) { indexes[2] = 64; indexes[3] = 64; } else if ((char)chars[2] == 0) { indexes[3] = 64; } foreach (int index in indexes) { result.Append(key[index]); } } while (i < input.Length); return result.ToString(); } |
Decode method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public static string Atom128Decode(string input) { string key = "/128GhIoPQROSTeUbADfgHijKLM+n0pFWXY456xyzB7=39VaqrstJklmNuZvwcdEC"; StringBuilder result = new StringBuilder(); int[] indexes = new int[4]; int[] chars = new int[3]; int i = 0; do { indexes[0] = key.IndexOf(input[i++]); indexes[1] = key.IndexOf(input[i++]); indexes[2] = key.IndexOf(input[i++]); indexes[3] = key.IndexOf(input[i++]); chars[0] = (indexes[0] << 2) | (indexes[1] >> 4); chars[1] = (indexes[1] & 15) << 4 | (indexes[2] >> 2); chars[2] = (indexes[2] & 3) << 6 | indexes[3]; result.Append((char)chars[0]); if (indexes[2] != 64) result.Append((char)chars[1]); if (indexes[3] != 64) result.Append((char)chars[2]); } while (i < input.Length); return result.ToString(); } |
Usage:
1 2 3 4 5 | // Results to LxrkpIQu0IHtOxSC+qCC+bCC string encodedString = Atom128Encode("fluxbytes.com"); // Results to fluxbytes.com string decodedString = Atom128Decode(encodedString); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 | [DllImport("user32.dll")] static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); private int SC_MONITORPOWER = 0xF170; private int WM_SYSCOMMAND = 0x0112; enum MonitorState { ON = -1, OFF = 2, STANDBY = 1 } |
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:
1 2 3 4 | public void SetMonitorState(MonitorState state) { SendMessage(this.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, (int)state); } |
Simply call the SetMonitorState
method with the desirable state you want to change your monitor’s state to.
Usage:
1 | SetMonitorState(MonitorState.OFF); |
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.
Posted in C#.
Tagged C#, csharp, HWND_BROADCAST, monitor, SC_MONITORPOWER, snippet, winforms, WM_SYSCOMMAND
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.
1 2 3 4 5 6 | public static bool IsUserAdministrator() { WindowsIdentity user = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(user); return principal.IsInRole(WindowsBuiltInRole.Administrator); } |
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.
- Right click on your project
- Click “Add Reference…”
- Select the “COM” tab on the left
- Search for Windows Script Host Object Model and add it as a 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:
1 2 3 4 5 6 7 8 9 10 11 | public static void CreateShortcut(string shortcutName, string shortcutPath, string targetFileLocation) { string shortcutLocation = System.IO.Path.Combine(shortcutPath, shortcutName + ".lnk"); WshShell shell = new WshShell(); IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation); shortcut.Description = "My shortcut description"; // The description of the shortcut shortcut.IconLocation = @"c:\myicon.ico"; // The icon of the shortcut shortcut.TargetPath = targetFileLocation; // The path of the file that will launch when the shortcut is run shortcut.Save(); // Save the shortcut } |
Usage:
1 | CreateShortcut("my shortcut", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Assembly.GetExecutingAssembly().Location); |
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:
1 2 3 4 5 6 7 | public static void AddApplicationToStartup() { using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.SetValue("My Program", "\"" + Application.ExecutablePath + "\""); } } |
Stop program from starting with Windows:
1 2 3 4 5 6 7 | public static void RemoveApplicationFromStartup() { using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.DeleteValue("My Program", false); } } |