Friday, August 27, 2010

Visual Studio Tip of the day - Format Document

Time and again we write code and our brackets get out of visual sync, i.e. they no longer appear as a coherent set even though they may be.

 

In Visual Studio, there is a feature known as Format Document which will align the code systematically.

 

It can be invoked by the key combination of Ctrl K + Ctrl D

 

Suppose you code looks like

 

namespace LogFileCheck

    {

    class Program

        {

        static void Main(string[] args)

        {

            TextReader sr = new StreamReader("mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000.log", Encoding.UTF8);

            TextWriter writesr =

                new StreamWriter("mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000_csResult.log",

                false,              Encoding.UTF8);

            while (sr.Peek()

                != -1)

                            {

                string line = sr.ReadLine();

                if (Regex.IsMatch(line, "&di=78") && Regex.IsMatch(line, @"([^,]*,){19}66"))

                    writesr.WriteLine(line);}

 

                sr.Close();

            writesr.Close();

        }

    }

}

 

Press the magic keys Ctrl K + Ctrl D and voila, all your code looks pretty organized as under:

 

namespace LogFileCheck

{

    class Program

    {

        static void Main(string[] args)

        {

            TextReader sr = new StreamReader("mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000.log", Encoding.UTF8);

            TextWriter writesr = new StreamWriter("mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000_csResult.log", false, Encoding.UTF8);

            while (sr.Peek() != -1)

            {

                string line = sr.ReadLine();

                if (Regex.IsMatch(line, "&di=78") && Regex.IsMatch(line, @"([^,]*,){19}66"))

                    writesr.WriteLine(line);

            }

 

            sr.Close();

            writesr.Close();

        }

    }

}

 

Want to format only a small selected section of the dirty code?  Select the area you want to format and press Ctrl K + Ctrl F.

 

1 comment:

Jeet Kansagara said...

Hi!!

A Nice Blog With Nice Posts Keep It Up.

Thanks & Regards,
Jeet Kansagara