Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

Find Hidden Files from Computer

4.85/5 (13 votes)
8 Nov 2013CPOL2 min read 19.8K   1.1K  
This article describes how to show file name extensions, hidden folders/files, and system folders/files in your system.

Sample Image

Introduction

You may have certain file name extensions hidden within Windows. We will see how to see the full file/folder names, extensions in the following section. I tried to make a simple app to find hidden files with a few clicks using C# code. If you know the name of a hidden file or folder, you can search for it.

Class for File information

First create a class to keep file information. Class details are shown below where it keeps extension, full name, file size, and if it is hidden etc.

C#
public class PcFile
{
    public string Extension { get; set; }
    public string FullName { get; set; }
    public string FileSize { get; set; }
    public long FileLength { get; set; }
    public DateTime CreationTime { get; set; }
    public string FileName { get; set; }
    public string Date { get; set; }
    public bool isReadOnly { get; set; }
    public bool isHidden { get; set; }
    public bool isArchive { get; set; }
    public bool isSystem { get; set; }
}

Using the design

In the design I have taken FolderBrowserDialog to choose a directory or drive, DataGridView to show the list of file details, radiobutton list all or hidden file option, treeview to get a folder hierarchy, and listbox to get extension wise files so the design of the page is as shown above.

Using the code

Code starts with a simple way where I browse the folder path and keeping txtPath.Text = folderBrowserDialog1.SelectedPath; then getting the folder hierarchy in a treeview with the following method:

C#
void ListDirectory()
{
    treeView1.Nodes.Clear();
    var rootDirectoryInfo = new DirectoryInfo(txtPath.Text);
    treeView1.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
    LoadFileDetails("");
}
TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
{
    var directoryNode = new TreeNode(directoryInfo.Name);
    try
    {
        foreach (var directory in directoryInfo.GetDirectories())
        {
            directoryNode.Nodes.Add(CreateDirectoryNode(directory));
            GetallFileInformation(directory.FullName);
        } 
    }
    catch (UnauthorizedAccessException) { }
    catch (Exception) { }
    return directoryNode;
}

In the above method we are collecting the folder hierarchy and calling the GetAllFileInformation method to store file information of current directories. See the following method where it is coded to keep file information.

C#
static List<PcFile> listParent = new List<PcFile>();
void GetallFileInformation(string path)
{
    try
    { 
        string[] files = Directory.GetFiles(path, "*.*", System.IO.SearchOption.TopDirectoryOnly);
        foreach (string file in files)
        {
            FileInfo info = new FileInfo(file);
            PcFile pc = new PcFile();
            pc.Extension = info.Extension;
            pc.FullName = info.FullName;
            pc.FileLength = info.Length;
            pc.CreationTime = info.CreationTime;
            pc.FileName = info.Name;
            pc.FileSize = String.Format(new FileSizeFormatProvider(), "{0:fs}", pc.FileLength);
            pc.isArchive = Common.isArchive(pc.FullName);
            pc.isHidden = Common.isHidden(pc.FullName);
            pc.isReadOnly = Common.isReadOnly(pc.FullName);
            pc.isSystem = Common.isSystem(pc.FullName);
            if (listParent.Exists(x => x.FullName == info.FullName) == false)
                listParent.Add(pc);
        }
    }
    catch { }
}

See in this method all file information are added to a static list of pcFile class. In this case we are loading all the file information the first time to a static list; after any indexing we just filter from this list. In case of hidden files, get files by extension or by folder and so on.

C#
lstExtentions.DataSource = (from f in listParent.AsParallel()
                                      select new { Extention = f.Extension.ToLower() }).
                                      OrderBy(x => x.Extention).Distinct().ToList();
var linqFileDetails = (from f in listParent.AsParallel()
                       select new
                       {
                           FileName = f.FileName,
                           FullName = f.FullName,
                           FileSize = f.FileSize,
                           CreationTime = f.CreationTime,
                           Extension = f.Extension
                       }).Distinct().ToList();
dgFileDetails.DataSource = linqFileDetails;

In another method filter its extensions and load into listbox where it will help us check by extension and another for the DataGridView to load some specific information. You will get its detail in the demo app or after downloading the code.

File View

Image 2

In the above picture you can mark hidden radio button is selected where extensions are showing those types of hidden files are exists in your selected folder. After that grid is loaded with gif extensions file on listbox item selection. See in this case if you are choosing any item from DataGridView if you are clicking on File Name it will open the selected file in your desktop. This is one easy solution to find hidden file and open that file.

History

In this article I have just tried to find the hidden type of files from your system. Download the demo project to check independently in your system. But for that you have to check if ASP.NET Framework 4.0 exists or not. If not download latest framework from the Microsoft website and install it into your system.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)