Category Archives: Visual Studio 2015 Tips

How to use common folder paths in C#

Reading files from application path may be one of key requirement of any application. Actually we can place many place you love to have, on a installation machine there can be restriction as well.

Like wise in a Windows system, installer may want to put apps and other files to Programfiles. This folder intended to work with executable,configuration files etc. You can’t programmatically create new folder or files in Programfiles folder, which may require some admin rights. So where we kept those documents and other data ?

It is wise to store such files on Application data folder or even on your Document folder. For reports and other stuffs Document folder is enough.

Access Special Folders

We can use the Enironment.SpecialFolder to access the App data and Documents and also can fetch path to those folders using GetFolderPath method of Environment class.

datapath = Environment.GetFolderPath(Environment.SpecialFolder
reportPath = Environment.GetFolderPath(Environment.

the first line will get the location of C:\Users\manoj\AppData\Roaming and the second will show the Document folder path .

Access application path

How to access application/executable path in C#. This can be done using Application.ExecutablePath and the GetFolderPath method


How to change the Back color of MDI Form – C#

As a C# programmer, you may have used Back Color property for changing the color of the background for control as well as Windows form. You can also found that this not works with MDI Parent form.

MDI is the container for child forms or control, actually, it’s inherited from the panel [leave it no…lol].

Ok, all you want to repaint all the controls in it, even though there are no controls were placed, think about the inherited panel. All we have to do is try to cast every control as MDIClient and loop through it and change the color as follows

MdiClient ctlMDI;

// Loop through all of the form's controls looking
 // for the control of type MdiClient.
 foreach (Control ctl in this.Controls)
 // Attempt to cast the control to type MdiClient.
 ctlMDI = (MdiClient)ctl;
 // Set the BackColor of the MdiClient control.
 ctlMDI.BackColor = Color.Gold;
 catch (InvalidCastException exc)
 // Catch and ignore the error if casting failed.

Place the code on Load Event of MDI Parent Form

How to Split strings using separator symbols in C#

We had some interesting string as follows which need to split and take the desired portion. C#.Net provides special string function called Split, which helps you broke complex strings into fragments.

Here are our Stings look like

121 | Whole Sale Rate

131 | Retail Rate

We begin with initialization of string and then build separator array, then call the split method. I guess you are familiar with the string array if you don’t just look at the 3rd line.

string  st=”121 | Whole Sale Rate”;

Here  “|” is the separator

string[] separator= { “|” };

string []selRateStr = null;

Let’s split the string with Split, which is the function available with string object also with ToString() too.

selRateStr =st.Split(separator,StringSplitOptions.None);

the first element in the selRateStr will be 121, which can be accessed as selRateStr [0];

Here is the authentic link from MSDN which may help you to learn Split function in detail.



C# Tips

Expensive strings and stringBuilder class in C#

As we know strings are a group of characters, usually composed of letters, numerals and special symbols etc. C# string type is immutable  object, i.e, they can be created once, after that they can’t be modified.

Diagnostic Tools in C#

Immutable string

Usually you may argue that it is a non sense, I am talking about. Let’s consider the following line of code

string str=” Some string” 

str+=” Some other string” 

Here firstly we created  string variable str and add some initial string value and trying to add  new string “Some other string” with the old one. Even Though there is + operator appeared after the variable, the string variable can’t be modified, instead C# will create a new string by combining the both strings. As a result more and more memory occupied.

Build mutable strings with StringBuilder

This problem can be simply fixed with in built C# class StringBuilder which is mutable object. Following example will clarify the  idea. Create a console application which will execute a series of sample string for a specific times, say 1000.

            string str=””;
for(int i = 0; i <= 10000; i++)
str += “Sample C# string”;
Console.WriteLine(“String numbers are:” + str);

Above code will result in extravagant use of memory and may slash the speed of the program, by using the string builder, it can be rewritten as ,

            string str=””;
StringBuilder sb = new StringBuilder() ;
for(int i = 0; i <= 10000; i++)
sb.Append (” Sample C# strings “);
str = sb.ToString();
Console.WriteLine(“String numbers are:” + str);

Open the Diagnostic Tool in Visual Studio 2013/15 to find out memory usage. As the program get more complex you can find out that StringBuilder uses less memory than immutable  string. StringBuilder also come with

Accessing child controls in C# with foreach

Simplest way to clear all combo Box/TextBox in Visual Studio C#  2015 is using foreach loop and moving through the control collection.

What about controls grouped within  a group box, things getting complicated,isn’t it? Let me explain how can it get done.

  • First we going through the Base control collection and get each and every control.
  • Secondly we are looking inside each controls if they have child.
  • We further search for the TextBox and ComboBox and do some formatting, or any usual activities you want.

Visual Studio 2015 Tips: Double press Tab after you type foreach for snippet to be inserted, so that you can do more with less time.