Some C# Tips

Introduction

Here you will some tips in C# including how to create a new folder, how to find if a file or folder exists or not, create a registry key and how to display time in a status bar.

Create a New Folder


System.IO.Directory.CreateDirectory(@"c:\NewFolder");
System.IO.Directory.CreateDirectory(@"c:\NewFolder\NewSubFolder"); 

Determine if a File Exists

bool exists;
exists = System.IO.File.Exists(@"c:\myfile.txt");
System.Console.Write(exists);Determine if a Folder Exists

Determine if a Folder Exists

bool
exists = System.IO.Directory.Exists(@"c:\my folder");
System.Console.Write(exists);

Create a Key In the Registry



Compiling the Code

  1. Copy the code and paste it into the Main method of a console application. 
  2. Replace the Names parameter with the name of a key that exists directly under the HKEY_CURRENT_USER node of the registry.
  3. Replace the Name parameter with the name of a value that exists directly under the Names node.

Display the Time in a StatusBar Control

private void timer1_Tick(object sender, System.EventArgs e)

{

    statusBar1.Panels[0].Text = DateTime.Now.ToShortTimeString();

}

Next Recommended Reading Cool Tips For A C# Coding Test