Sunday, 19 August 2018

Show Progress bar in console application c#

In this post we are going to see how to show the progress bar in console application c#. For that we have to set the cursor in the console application to move in to position , then we have to set the background color .

Source code:
***********

        static void Main(string[] args)
        {
            Console.WriteLine("Uploading inprogress....");
            Console.WriteLine();
            for (int i = 0; i <= 100; i++)
            {
                ShowProgressBar( i, 100);
                Thread.Sleep(500);
            }
            Console.Read();

        }


        private static void ShowProgressBar(int progress, int total)
        {
            //draw empty progress bar
            Console.CursorLeft = 0;
            Console.Write("[");
            Console.CursorLeft = 32;
            Console.Write("]");
            Console.CursorLeft = 1;
            float oneunit = 30.0f / total;

            //draw progress
            int position = 1;
            for (int i = 0; i < oneunit * progress; i++)
            {
                Console.BackgroundColor = ConsoleColor.Green;
                Console.CursorLeft = position++;
                Console.Write(" ");
            }

            //draw strip bar
            for (int i = position; i <= 31; i++)
            {
                Console.BackgroundColor = ConsoleColor.Gray;
                Console.CursorLeft = position++;
                Console.Write(" ");
            }

            //draw totals
            Console.CursorLeft = 35;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Write(progress.ToString() + " of " + total.ToString() + "    ");

        }




Output:
***********





From this post you can learn how to show the progress bar in console application c#.