Click here to Skip to main content
13,049,263 members (62,877 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 28 Feb 2012

A Tiny Tool to Unhide Files on a USB Drive

, 30 Mar 2012
Rate this:
Please Sign up or sign in to vote.
A tiny tool to unhide files on a USB drive.

At times computer viruses reset file attributes in USB drives. Some of them create a shortcut using the name of the file to link to the virus’s name, while others simply hide your file. In this article I will list a small source code to an application for this operation. Though, my main intention is not to explain about the action of the scripts, it is vital to know what the scripts do.

In most cases viruses reset or add hidden, system, and read-only attributes to files and directories. As a computer professional I have seen many novice users face problems trying to reset the attribute of these files. It will be handy to have some utility to return files to their normal view using a single command in the command prompt. Many end users feels aghast by the black screen and type DOS commands in the shell. They feel arduous to remember a single command, i.e., attribute –s -r –h /s /d.


I took a commence approach to make the command easier using a simple C# code. To make it short, the program is a simple UI which iterates over the removable device. Once the user selects the USB drive and clicks the Reset button the files in the USB are reset to the normal (archive only) attribute.

The application can be done using different ways such as:

  • calling a batch command from the program
  • using the System.IO namespace

I chose to develop it using the System.IO namespace with Microsoft Input/ output namespace as it is flexible enough to play around.

The fun things I do in this program are mainly in two functions:

  • A recursive function to process the files and directories on the USB drive
  • The ResetAttribute function which resets a file or directory to normal attributes (archive) type.

Using the Code

The Recursive Scan Function

/// <summary>
/// </summary>
/// <param name="currDir">Drive or Directory path</param>
private void ScanFiles(string currDir)
    if(!Directory .Exists (currDir ))
    //set the prograss bar max value
    pbar.Maximum += Directory.GetFileSystemEntries(currDir).Count();
    //iterate over the files
    foreach (string ff in Directory.GetFiles(currDir))
        //super cast to the parent class FileSystemInfo
        ResetAttribute((new FileInfo(ff)) as FileSystemInfo );
    foreach (string dr in Directory.GetDirectories(currDir))
        if (!Directory.Exists(dr))
        //reset the directory attribute
        ResetAttribute((new DirectoryInfo(dr)) as FileSystemInfo);
        ScanFiles(dr); //scan the directory 

Here, to reset the file and directory attribute, an associate object is created, then this is super cast to the parent abstract class of DirectoryInfo and FileInfo which is FileSystemInfo.

ResetAttribute((new DirectoryInfo(dr)) as FileSystemInfo);

Similarly for the file also:

ResetAttribute((new FileInfo(ff)) as FileSystemInfo);

ResetAttribute Function

The second core part is the method ResetAttribute for resetting the attributes of the file.

/// <summary>
/// Remove file attribe of s,h and r
/// </summary>
/// <param name="fsi"> fileSysteminfo type object</param>
private void ResetAttribute(FileSystemInfo fsi)
    { //bitwise operator on the file attribute
        fsi.Attributes &= ~FileAttributes.Hidden;
        fsi.Attributes &= ~FileAttributes.System;
        fsi.Attributes &= ~FileAttributes.ReadOnly;
    pbar.Value += 1;
    catch (Exception ex)

This function removes the attribute of a file when any of the attribute type system, hidden or read-only, are set on the file. Since both DirectoryInfo and FileInfo are subclasses of FilesystemInfo, I chose this method over overload methods to accept a FileInfo type and DirectoryInfo type.

The resetting operation is done using a bitwise operation where an AND operation is done with a complement of the attribute type. Since an AND operation is true only if both operands are true, we complement the attribute to yield a false operand, i.e., ~FileAttributes.Hidden. So whatever the previous attribute of the file is, the attribute is reset (false).


In conclusion, I developed this small application to be a handy and easy to use tool for novice computer users. I hope this will alleviate some of the common questions from users to sysadmins.


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


About the Author

Software Developer
Eritrea Eritrea
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 30 Mar 2012
Article Copyright 2012 by tadeze
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid