18 Jun

C# Notify Icon Example

A notification icon notifies the user. In Windows there is a Notification Icons section, typically in the bottom right corner. To create a notify icon application, we use NotifyIcon instance in System.Windows.Forms namespace.

Example Code:

            NotifyIcon trayIcon = new NotifyIcon();    
            trayIcon.Icon = new Icon(@"C:\csharp.ico");
            trayIcon.Text = "New message";
            trayIcon.Visible = true;
            trayIcon.ShowBalloonTip(2000, "Information", "A new message received!", ToolTipIcon.Info);
screenshot of notify icon

screenshot of notify icon

17 Jun

Get Enum Description From Value in C#

The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. We can define the attributes for enum and if you want to fetch these description simply, we can use as shown below.

Usage:

            string description = EnumHelper.StringValueOf(SiteType.Technology);
            Console.WriteLine(description);
            //Output:
            //This is a technology site.

Sample Code:

    public class EnumHelper
    {
        public static string StringValueOf(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
            {
                return attributes[0].Description;
            }
            else
            {
                return value.ToString();
            }
        }
    }
17 Jun

Print Fibonacci Sequence With Recursive And Iteratively In C#

Fibonacci sequence is a sequence of numbers where the next number is the sum of the previous two numbers behind it. It has its beginning two numbers predefined as 0 and 1. The sequence goes on like this:
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377…

Usage:

            Console.WriteLine("Iterative:");
            for (int i = 0; i < 15; i++)
            {
                Console.Write(CalculateFibonacciNumberIteratively(i) + " ");
            }

            Console.WriteLine();
            Console.WriteLine("Recursive:");
            for (int i = 0; i < 15; i++)
            {
                Console.Write(CalculateFibonacciNumberRecursively(i) + " ");
            }

            //Output window is like follow.

            //Iterative:
            //0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
            //Recursive:
            //0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Iterative Method:

        private int CalculateFibonacciNumberIteratively(int n)
        {
            int result = 0;
            int previous = 1;

            for (int i = 0; i < n; i++)
            {
                int temp = result;
                result = previous;
                previous = temp + previous;
            }
            return result;
        }

Recursive Method:

        private int CalculateFibonacciNumberRecursively(int n)
        {
            if (n == 0)
                return 0;
            else if (n == 1)
                return 1;
            else
                return CalculateFibonacciNumberRecursively(n - 2) + CalculateFibonacciNumberRecursively(n - 1);
        }
16 Jun

Download Files Synchronous And Asynchronous From A URL in C#

This page tells how to download files from any url to local disk. To download the files, We use WebClient class in System.Net namespace. This class supports to the synchronous and asynchronous downloads. Following examples show how to download files as synchronous and asynchronous.

This example downloads the resource with the specified URI to a local file.

            using (WebClient webClient = new WebClient())
            {
                webClient.DownloadFile("http://www.example.com/file/test.jpg", "test.jpg");
            }

This example downloads to a local file, the resource with the specified URI. Also this method does not block the calling thread.

            using (WebClient webClient = new WebClient())
            {
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(delegate(object sender, DownloadProgressChangedEventArgs e)
                    {
                        Console.WriteLine("Downloaded:" + e.ProgressPercentage.ToString());
                    });

                webClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler
                    (delegate(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
                    {
                        if(e.Error == null && !e.Cancelled)
                        {
                            Console.WriteLine("Download completed!");
                        }
                    });
                webClient.DownloadFileAsync(new Uri("http://www.example.com/file/test.jpg"), "test.jpg");
            }
16 Jun

C# Convert A Numeric Value To Hexadecimal Format

This example shows a numeric value in its hexadecimal format. To do this, we use the x or X formatting specifier. The letter case of the formatting specifier determines whether the hexadecimal letters appear in lowercase or uppercase.

Sample Code 1:

            // Convert numeric value to hexadecimal value
            Console.WriteLine(string.Format("0x{0:X}", 12));
            Console.WriteLine(string.Format("0x{0:X}", 64));
            Console.WriteLine(string.Format("0x{0:X}", 123));
            Console.WriteLine(string.Format("0x{0:X}", 196));
            Console.WriteLine(string.Format("0x{0:X}", 255));
            //Output:
            //0xC
            //0x40
            //0x7B
            //0xC4
            //0xFF

If we want to convert hexadecimal value to numeric value, we can use like below:

Sample Code 2:

            //Convert hexadecimal value to numeric value
            Console.WriteLine(Convert.ToInt32("0xC", 16).ToString());
            Console.WriteLine(Convert.ToInt32("0x40", 16).ToString());
            Console.WriteLine(Convert.ToInt32("0x7B", 16).ToString());
            Console.WriteLine(Convert.ToInt32("0xC4", 16).ToString());
            Console.WriteLine(Convert.ToInt32("0xFF", 16).ToString());
            //Output:
            //12
            //64
            //123
            //196
            //255