Tag Archives: C#
Monitor for clipboard changes using AddClipboardFormatListener
Microsoft has added a new windows function to help monitor when the data in the clipboard has been changed. The new function is called AddClipboardFormatListener but sadly it is only available for Windows Vista and higher. If you are looking for a method that will work for earlier versions of Windows take a look at Monitor clipboard in C#.
The principle is the same with the older method. We need to add our window to the clipboard format listener list so it can receive the WM_CLIPBOARDUPDATE message.
In order to do that we need to pinvoke the AddClipboardFormatListener
and RemoveClipboardFormatListener
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /// <summary> /// Places the given window in the system-maintained clipboard format listener list. /// </summary> [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool AddClipboardFormatListener(IntPtr hwnd); /// <summary> /// Removes the given window from the system-maintained clipboard format listener list. /// </summary> [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool RemoveClipboardFormatListener(IntPtr hwnd); /// <summary> /// Sent when the contents of the clipboard have changed. /// </summary> private const int WM_CLIPBOARDUPDATE = 0x031D; |
Posted in C#.
Tagged AddClipboardFormatListener, C#, csharp, RemoveClipboardFormatListener, snippet, winforms, WM_CLIPBOARDUPDATE
How to get your IP in C#
There are two ways of getting your IP address in C#. One of them is by using the methods provided by the .NET framework and the other one by using a third party api.
What is the difference you might ask. The answer is fairly simple. There are two kinds of IP’s, internal IP’s and external IP’s. Sadly the .NET framework methods are not able to retrieve the external IP if the computer is not directly connected to the internet (no router or any other similar devices). Since this limitation exists there is only one option if you wish to get the external IP, and this is by using an online service.
I’ve constructed two methods below. One uses only the .NET framework methods but is not 100% accurate when trying to retrieve the external IP (requires the system to be directly connected to the internet). The second method uses an online service which should have fairly accurate results as long as the service is online and the computer is not behind a proxy.
Method 1
LINQ approach
1 2 3 4 5 6 | IPAddress localAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork); if (localAddress != null) { // do something with localAddress } |
Non-LINQ approach (for .NET framework versions before 3.5)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | IPAddress localAddress = null; foreach (IPAddress address in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (address.AddressFamily == AddressFamily.InterNetwork) { localAddress = address; break; } } if (localAddress != null) { // do something with localAddress } |
Method 2
1 2 3 4 5 6 7 8 9 10 | string ipAddress; using (WebClient wc = new WebClient()) { ipAddress = wc.DownloadString("http://icanhazip.com/"); } if (!string.IsNullOrEmpty(ipAddress)) { // do something with ipAddress } |
RC4 cypher in C#
Main 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 | public static string RC4(string input, string key) { StringBuilder result = new StringBuilder(); int x, y, j = 0; int[] box = new int[256]; for (int i = 0; i < 256; i++) { box[i] = i; } for (int i = 0; i < 256; i++) { j = (key[i % key.Length] + box[i] + j) % 256; x = box[i]; box[i] = box[j]; box[j] = x; } for (int i = 0; i < input.Length; i++) { y = i % 256; j = (box[y] + j) % 256; x = box[y]; box[y] = box[j]; box[j] = x; result.Append((char)(input[i] ^ box[(box[y] + box[j]) % 256])); } return result.ToString(); } |
Usage:
1 2 3 4 5 | // Results to º§(ÓM¦Kéµ(äþ« string cypheredText = RC4("fluxbytes.com", "mykey"); // Results to fluxbytes.com string deCypheredText = RC4(cypheredText, "mykey"); |
ROT-13 cypher in C#
ROT-13 is a letter substitution cypher that replaces a letter with the letter 13 letters after it in the alphabet. ROT-13 is an example of the Caesar cypher with the difference that Caesar cypher allows you to specify the number of letters to shift.
Main method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static string Rot13(string input) { StringBuilder result = new StringBuilder(); Regex regex = new Regex("[A-Za-z]"); foreach (char c in input) { if (regex.IsMatch(c.ToString())) { int charCode = ((c & 223) - 52) % 26 + (c & 32) + 65; result.Append((char)charCode); } else { result.Append(c); } } return result.ToString(); } |
Usage:
1 2 3 4 5 | // Results to syhkolgrf.pbz string cypheredText = Rot13("fluxbytes.com"); // Results to fluxbytes.com string deCypheredText = Rot13(cypheredText); |