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
private void ScanFiles(string currDir)
{
if(!Directory .Exists (currDir ))
return;
pbar.Maximum += Directory.GetFileSystemEntries(currDir).Count();
foreach (string ff in Directory.GetFiles(currDir))
{
ResetAttribute((new FileInfo(ff)) as FileSystemInfo );
}
foreach (string dr in Directory.GetDirectories(currDir))
{
if (!Directory.Exists(dr))
return;
ResetAttribute((new DirectoryInfo(dr)) as FileSystemInfo);
ScanFiles(dr);
}
}
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.
private void ResetAttribute(FileSystemInfo fsi)
{
try
{
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.