Tag Archives: csharp
Shortening a URL using bitly’s API in C#
Using the snippet below you can convert links in your application from this
1 | http://www.fluxbytes.com/ |
to this
1 | http://bit.ly/WVk1qN |
This is especially important in cases where you have a fairly long URL which you want to post somewhere and you either don’t want to use such a long URL, or simply you are limited by characters. An example of that situation would be a URL such as
1 | http://www.google.com/url?sa=t&rct=j&q=c+sharp+convert+string+to+binary&source=web&cd=1&cad=rja&ved=0CDQQFjAA&url=http%3A%2F%2Fwww.fluxbytes.com%2Fcsharp%2Fconvert-string-to-binary-and-binary-to-string-in-c%2F&ei=T79SUcOVI4rEPOeogPgP&usg=AFQjCNEihqo_KsRZuGpb0-ZpWdqjdqr_sA |
which can be shortened down to
1 | http://bit.ly/WVuXF4 |
The first thing you are going to need it an account from bitly.com. After you have created an account, log in and navigate to https://bitly.com/a/your_api_key. The page will contain your username and your API key which are both needed.
Convert string to binary and binary to string in C#
The following two snippets allow you to convert a string to binary text and also to convert binary back to string.
String to binary method:
1 2 3 4 5 6 7 8 9 10 | public static string StringToBinary(string data) { StringBuilder sb = new StringBuilder(); foreach (char c in data.ToCharArray()) { sb.Append(Convert.ToString(c, 2).PadLeft(8, '0')); } return sb.ToString(); } |
Binary to string method:
1 2 3 4 5 6 7 8 9 10 | public static string BinaryToString(string data) { List<Byte> byteList = new List<Byte>(); for (int i = 0; i < data.Length; i += 8) { byteList.Add(Convert.ToByte(data.Substring(i, 8), 2)); } return Encoding.ASCII.GetString(byteList.ToArray()); } |
Usage:
1 2 3 4 5 | // returns "01100110011011000111010101111000011000100111100101110100011001010111001100101110011000110110111101101101" StringToBinary("fluxbytes.com") // returns "fluxbytes.com" BinaryToString("01100110011011000111010101111000011000100111100101110100011001010111001100101110011000110110111101101101"); |
Posted in C#.
Tagged binary, binary to string, C#, csharp, snippet, string to binary, winforms
Create and extract .zip files in C#
Sadly there aren’t many flexible or efficient ways to create .zip files in .NET prior to .NET 4.5. Thankfully some people took the initiative and created some very easy to use libraries for creating/extracting and updating .zip files. My two all time favourite are DotNetZip and SharpZipLib.
For this example I will be using the DotNetZip
library.
First you will need to download the library (.dll) either from http://dotnetzip.codeplex.com/ or from http://www.fluxbytes.com/?dl_name=DotNetZipLib_v1.9.1.8.rar. The file should contain quite a few libraries, so choose the one that suits your needs the most and add it as a reference in your project.
Posted in C#.
Tagged C#, csharp, DotNetZipLib, SharpZipLib, snippet, tutorial, winforms
How to register a global hotkey for your application in C#
If you are looking for a way to set a global hotkey for your C# application that can be used without your form having focus I have created a decently commentated example for you below that you can use.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | using System; using System.Windows.Forms; namespace GlobalHotkeyExampleForm { public partial class ExampleForm : Form { [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk); [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); enum KeyModifier { None = 0, Alt = 1, Control = 2, Shift = 4, WinKey = 8 } public ExampleForm() { InitializeComponent(); int id = 0; // The id of the hotkey. RegisterHotKey(this.Handle, id, (int)KeyModifier.Shift, Keys.A.GetHashCode()); // Register Shift + A as global hotkey. } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == 0x0312) { /* Note that the three lines below are not needed if you only want to register one hotkey. * The below lines are useful in case you want to register multiple keys, which you can use a switch with the id as argument, or if you want to know which key/modifier was pressed for some particular reason. */ Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); // The key of the hotkey that was pressed. KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF); // The modifier of the hotkey that was pressed. int id = m.WParam.ToInt32(); // The id of the hotkey that was pressed. MessageBox.Show("Hotkey has been pressed!"); // do something } } private void ExampleForm_FormClosing(object sender, FormClosingEventArgs e) { UnregisterHotKey(this.Handle, 0); // Unregister hotkey with id 0 before closing the form. You might want to call this more than once with different id values if you are planning to register more than one hotkey. } } } |
Posted in C#.
Tagged C#, csharp, global hotkey, RegisterHotKey, snippet, UnregisterHotKey, winforms