Spiga

Cool File System Class with Thread Support - Easy To Use

FileSystem is a class which will enable you to quickly, easily and efficiently get the content of a given directory. A file system class is an essential part of any code library. Every application does some sort of file interaction, and if you create a nice class to handle interaction between your application and the file system then it will make life a bit easier and you will not need to recreate the wheel a hundred times for each project. Here I will try to provide such a class that you can use in your project. This article is an extension of another article (Automating MS Excel 2003 using Visual Studio .NET), which are both part of the same project. However, I have done some modifications to the code provided here that will make it more generic.

So there are two section to this article, one which will cover the FileSystem class and the other that will cover the application that will use the class to demonstrate some of its features.

Background
No background needed, just be familiar with the C# language, threads, and FileSystemObject.

Using the code
Here I will try to cover the FileSystem class. This class is really simple and self explanatory. It is also well documented. But let's go over it in any case. So we have a class named FileSystem that is used to do some file system operations. Let's take a look at the class:

FileSystem Class

Alright, so we have several files that are used internally by the class. Six of them are available as properties that can be accessed at runtime and a few are set during initialization, i.e., sourcePath and destinationPath.

COPY_STATUS - is used to notify the parent thread about the status of the copy thread.
FILE_FILTER - is used for filtering file types. i.e., *.xml, *.cs, *.xls, etc... if left null it will behave as *.*
NUMBER_OF_DIRECTORIES - returns number of directories in the search. NUMBER_OF_FIELS - returns number of files in the search.
ROOT_PATH - gets or sets the root path to be used for navigation.
STATUS - is the status of the main thread in the FileSystem class.
You also can notice that there are three private methods that are used internally by the class. Two of those methods are used to start a particular thread. And the RecursiveDirectoryNavigation() method is used by the main thread of the FileSystem class to do the navigation, given the rootPath.

To use the copy method of the class you will use one of the two overloaded functions for the constructor and then will call the copyDirectory method which will initiate the startCopyThread to perform the operation. From your main thread you can use the COPY_STATUS property to check on the thread for your next program logic if any.

To use the code, all you need is the following:

CoolFileSystem.FileSystem fileSystem

= FileSystem(@root); That's all it takes! Below you will find code snippets. Of course the full source is available for you to download. Below you can see a portion of the class source.

namespace CoolFileSystem {

public class FileSystem

{

// Used to hold root directory. Start point for the search.

private string rootPath;

...

// Used to filter filename when doing

// the search. By default it is set to "*.xls".

// Can be changed using the FILE_FILTER property.

private string fileFilter = "*.*";

// File list is used internally. It is a list

// of all files found given the search

// criteria. Automatically adjust itself. Default size 500.

private ArrayList fileList = new ArrayList(500);

// Used to store directories found under

// the rootPath. It contains objects of type

// DirectoryStructure which hold

// the Directory Information with a list of files

// under the current directory.

private ArrayList directoryList = new ArrayList(500);

// Used for navigation thru the directory structure.

private Thread mainThread;

...

...

public FileSystem(string path)

{

rootPath = path;

mainThread = new Thread(new ThreadStart(startThread));

mainThread.Start();

}

private void startThread()

{

DirectoryInfo di = new DirectoryInfo(rootPath);

RecursiveDirectoryNavigation( di );

}

...

...

#region RECURSIVE DIRECTORY NAVIGATION

[STAThread]

private void RecursiveDirectoryNavigation( DirectoryInfo di )

{

try

{

// Add the current directory to the directory list

DirectoryStructure ds = new DirectoryStructure();

ds.di = di;

int insertionIndex = 0;

// Let's get files in directory

foreach( FileInfo fi in di.GetFiles(fileFilter) )

{

fileList.Insert(insertionIndex, fi);

ds.fileList.Insert(insertionIndex, fi);

}

directoryList.Add(ds);

foreach( DirectoryInfo d in di.GetDirectories() )

{

RecursiveDirectoryNavigation( d );

}

}

catch {}

finally {}

}