Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / C#

A Tiny Tool to Unhide Files on a USB Drive

Rate me:
Please Sign up or sign in to vote.
4.92/5 (7 votes)
30 Mar 2012CPOL3 min read 27K   1.8K   14  
A tiny tool to unhide files on a USB drive.

Image 1

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.

Background

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

C#
/// <summary>
///
/// </summary>
/// <param name="currDir">Drive or Directory path</param>
private void ScanFiles(string currDir)
{ 
    if(!Directory .Exists (currDir ))
        return;
    //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))
            return;
        //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.

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

Similarly for the file also:

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

ResetAttribute Function

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

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

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).

Conclusion

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.

License

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


Written By
Software Developer
Eritrea Eritrea
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --