|
|||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Introduction
So there are two section to this article, one which will cover the BackgroundNo background needed, just be familiar with the C# language, threads, and Using the codeHere I will try to cover the
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 {}
}
#endregion
public ArrayList GetFiles()
{
return fileList;
}
public ArrayList GetDirectories()
{
return directoryList;
}
public string GetNumOfFilesCopied()
{
return copyNumofFiles.ToString();
}
}
...
So from above code, you get a preview of the class. The code is well documented so I will not get into the details. In the following section, I will show you how the class is used in the main code or the driver. In this case, I created a Windows application which has a button, and some labels that show the status of the class. When the button is clicked, you get the
namespace CoolFileSystem
{
/// <summary>
/// Driver for CoolFileSystem
/// </summary>
public class DriverCoolFileSystem : System.Windows.Forms.Form
{
// Declare variable for CoolFileSystem
private CoolFileSystem.FileSystem fileSystem = null;
...
public DriverCoolFileSystem()
{
InitializeComponent();
}
...
private void butRootDirectory_Click(object sender, System.EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
DialogResult result = folderBrowserDialog.ShowDialog();
if( result == DialogResult.OK )
{
if( this.fileSystem == null )
{
this.fileSystem =
new FileSystem(@folderBrowserDialog.SelectedPath);
timer.Start();
}
else
{
MessageBox.Show( "File Sys running ...");
}
}
}
private void timer_Tick(object sender, System.EventArgs e)
{
this.lblDirectories.Text = "Num of Directories: " +
this.fileSystem.NUMBER_OF_DIRECTORIES;
this.lblFiles.Text = "Num of Files: " +
this.fileSystem.NUMBER_OF_FILES;
this.lblStatus.Text = "Status: " + this.fileSystem.STATUS;
if( this.fileSystem.STATUS.Equals("Stopped") )
{
this.lblStatus.Text = "Status: " + this.fileSystem.STATUS;
timer.Stop();
// populate the tree view
Thread listFolders =
new Thread(new ThreadStart(ListDirectories));
treeView.Nodes.Clear();
listFolders.Start();
}
}
private void ListDirectories()
{
ArrayList directoryList = this.fileSystem.GetDirectories();
foreach( DirectoryStructure ds in directoryList )
{
if( ds.fileList.Count > 0 )
{
TreeNode folder = new TreeNode(ds.di.Name.ToString());
foreach( FileInfo fi in ds.fileList )
{
TreeNode file = new TreeNode(fi.FullName);
folder.Nodes.Add(file);
}
// Vahe Karamian - 03/01/2005 - This is for Thread Safety
if(treeView.InvokeRequired == true)
{
// check if we running within the same thread
treeView.Invoke(new AddToTreeView(treeView.Nodes.Add),
new object[] {folder});
}
else
{
treeView.Nodes.Add(folder);
}
}
}
this.fileSystem = null;
}
}
}
If you notice, there is a timer which is set to check the status of the file system thread every second. Again, the code is simple and I will not get into the details. I hope that this code sample gives yet a different approach to doing file system navigation. Points of InterestThe class is very flexible and you can easily extend it and use it in your own programs. One of the features which I removed from the current release for CodeProject is the ability to search for the same type of files and get the latest one for processing. There are some minor adjustments you will need to do in order to get the functionality. What I provided here is a general code base that can be used by everyone.
|
||||||||||||||||||||||||||||||||||||||||||||||||