Thursday 1 October 2015

Enumerate folder with “No Name” (blank space folder, AKA non-breaking space) in C#.Net:

A folder with no name can be created by following the below steps:
  1. Right-click on the folder and select Rename.
  2. Now press the Alt key and from the Numeric keypad, press 0160.
  3. Now press Enter or click anywhere on the desktop.


If you want to retrieve all the files and folders from any directory (in C#), you would do something like this:

string path = “Path to the directory”;
DirectoryInfo di = new DirectoryInfo(path);
foreach (DirectoryInfo dirInfo in di.GetDirectories())
{
       FileInfo[] fInfos = dirInfo.GetFiles();
       // You can easily iterate through the files here
}

But if you have any folder with a blank space as name, the above method would give you wrong result. You would need to make few changes as shown below:

string path = “Path to the directory” + “\\”;
DirectoryInfo di = new DirectoryInfo(path);
foreach (DirectoryInfo dirInfo in di.GetDirectories())
{
     if (dirInfo.Name == "\u00A0")
     {
          string fullPath = dirInfo.FullName + "\\";
          dirInfo = new DirectoryInfo(fullPath);
     } 

     FileInfo[] fInfos = dirInfo.GetFiles();
     // You can easily iterate through the files
}
  

Reduce flickering from controls in C#.Net

Flickering effect can be reduced by double buffering the controls. The below code will help you in reducing the flickering effect from any type of control.

public static class ControlExtensions
    {
        public static void SetDoubleBuffered(System.Windows.Forms.Control c)
        {
            if (System.Windows.Forms.SystemInformation.TerminalServerSession)
                return;

            System.Reflection.PropertyInfo aProp =
                  typeof(System.Windows.Forms.Control).GetProperty(
                        "DoubleBuffered",
                        System.Reflection.BindingFlags.NonPublic |
                        System.Reflection.BindingFlags.Instance);

            aProp.SetValue(c, true, null);
        }
    }

Whether it is a panel, or a Tab control, you can call the above defined function:

ControlExtensions.SetDoubleBuffered(MenuPanel);
ControlExtensions.SetDoubleBuffered(settingsTabControl);