Move files and folder from source location to destination location Move files/folders from old folder to new Folder recrussively. private static void MoveAll(string pobjSource, string pobjTarget)

{

// Check if the target directory exists, if not, create it.

if (Directory.Exists(pobjTarget) == false)

{

Directory.CreateDirectory(pobjTarget);

} foreach (string filename in Directory.GetFiles(pobjSource, “*.*”))

{

File.Copy(filename, Path.Combine(pobjTarget, Path.GetFileName(filename)));

} // Copy each subdirectory using recursion.

foreach (string objSourceSubDir in Directory.GetDirectories(pobjSource))

{

MoveAll(objSourceSubDir, Path.Combine(pobjTarget, Path.GetFileName(objSourceSubDir)));

}

}

Static Readonly vs Constant Static Readonly vs Constant Static ReadOnly >> Static readonly is a class level variable that can’t be accessed using the class object. It’s value can bedefined at the runtime using any expression or any run time variable only in the static constructor of the class. Constant >> Constants are also class level variables as they are implicitly static but they can be set only once (at the time of declaration only). You can’t set a constant at the runtime. If you want to define your field at design time then you must use constant but if the value of the field is decided at the runtime then you have the only option to use Static readonly.

Customize Tabs in TabControl By default tabs of a tab control are set to top, but you have following options to change its alignment. Top: Tabs appear on the top of the tab control. Bottom: Tabs appear on the bottom of the tab control. Left: Tabs appear on the left of the tab control. Right: Tabs appear on the right of the tab control.

Multiple Main() Methods We can have multiple main methods in our console/windows application.

But a class can’t have more than one main method. Steps to set startup main method VS2005: 1. Go to solution properties.

2. Select application tab.

3. Set startup object from the dropdown list, if you don’t do so your application will not compile.

Generating incremental file name Following code could help you in getting Incremental or Unique file name. If file abc.txt already exist than following function will return abc1.txt, if abc1.txt exists than abc2.txt and so on. public static string GetIncrementalFilename(string FileName)

{

int count = 0;

string Name = “”; if (System.IO.File.Exists(FileName))

{

System.IO.FileInfo f = new System.IO.FileInfo(FileName);

if (!string.IsNullOrEmpty(f.Extension))

{

Name = f.FullName.Substring(0, f.FullName.LastIndexOf(‘.’));

}

else

{

Name = f.FullName;

} while (File.Exists(FileName))

{

count++;

FileName = Name + count.ToString() + f.Extension;

}

}

return FileName;

}

Line Numbers in C# code Editor How to enable line numbers in C# code editor? Step1:

(Refer image Step1)

Go to Tools > Options Step2:

(Refer image Step2)

Go to C# and then under display option check Line number checkbox and you are done with it.



Thread Safe Singleton Pattern: namespace Organization

{

class Employee

{

//Cretae a static object of the class

private static Employee _employee = null; private static object padlock = new object(); /// <summary>

/// This method will be called

/// whenever an instance of the class will be needed

/// </summary>

/// <returns>Single instance of Employee Class</returns>

public static Employee GetInstance()

{ lock(padlock)

{

if (_employee == null)

{

_employee = new Employee();

} }

return _employee } /// <summary>

/// Default constructor is specified as private

/// So that it coldnot be used from outside

/// </summary>

private Employee()

{ }

}

}