Feb 27, 2012

A tiny Tool to unhide hidden files on USB drive

At the time many computer viruses reset the attribute of files in USB drive.  Some of them create a shortcut using the name of the files to link to the virus’s name, while others simply hide your file.  In this article I will note a small source code to an application for this operation. Though, my main intention is not explain about the action of the scripts, it is vital to know what the scripts do.
In most cases the operation by the viruses to reset or add hidden, system and read-only attributes to the files and directory. As a computer professional I have seen also many novice users to face problem to reset the attribute of these files. It is also a handy to return files to its normal view using single command in command prompt. Unlikely, many end users feels aghast by the black screen and typing dos command in the shell. They feel arduous to remember the single command i.e. attribute –s  -r –h /s /d.
I took commence approach to make the command easier using simple C# codes. To make it short, the program is simple UI which iterates over the removable device. Once the user select the USB drive and click the reset button the whole files in the USB reset to normal (archive only) attribute.
Although, the application can be done using different ways such as
-          calling batch command from the program
-          using system.IO namespace
I choose 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 the two functions.
-          The recursive function to process the whole files and directory on the USB drive
-          The Reset Attribute function which reset a file or directory to normal attributes (archive) type.
1.     The Recursive Scan Function


        /// Drive or Directory path
        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 when to reset the file and directory attribute an associate object is created then this is super casted to the parent abstract class of DirectoryInfo and FileInfo which is FileSystemInfo
1.   i.e. ResetAttribute((new DirectoryInfo(dr)) as FileSystemInfo);

Similarly for the file also.
2.  ResetAttribute((new FileInfo(ff)) as FileSystemInfo );

2.ResetAttribute Function

The second core part is the method ResetAttribute; which function for resetting the attributes of the file


        /// Remove file attribe of s,h and r

        /// fileSysteminfo type object
       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 attribute type system, hidden and read-only are set on the file. Since both DirectoryInfo and FileInfo are subclass of FilesystemInfo, I chose this method over overload methods to accept a FileInfo type and DirectoryInfo type.

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

Inconclusion , I developed this small application to be a handy and easy to use for the lame computer users. I hope this will alleviate some of the common inquiry from users to sysadmin.

Source Code 
There was an error in this gadget