Reputation: 3789
I got a few classes, which look into directories and subdirectories to check if the structure of these folders and files fit to the class-requirements. E.G. I got a JPEG-Class which looks in all subdirectories for any .jpeg. Some classes are even more complex where some directories and subdirectories need to have specific names and should contain some files with specified extensions.
Therefore I currently have the problem that I need to read the file system more than once to do these calculations as the classes have different requirements which can't be solved in one loop.
What I'm doing so far: I've implemented a loop over the file system with Directory.GetFiles(path)
and Directory.GetDirectories(path)
for every class and check if the structure is readable by my class
My question is: How can I improve my performance. I think it would be a big performance increase if I can store the file-system (directories and subdirectories from the selected path) into my memory, maybe in a tree-structure to avoid multiple file-system-calls? Is this the best behaviour? Or do you think this wouldn't bring a performance boost? Are there other ways I could do to get some improvements regarding the performance?
If the tree is the best solution - how would you implement it?
public class TreeNode
{
List<TreeNode> nodes;
FileInfo currentFileInfo;
DirectoryInfo currentDirectoryInfo;
}
Is this structure suitable for my issue?
Regards
Upvotes: 1
Views: 161
Reputation: 3710
I have successfully improved performance earlier by reading all the file information first and analyze it later by different classes. According to your question, yes, I'd recommend doing so.
I cannot know how your "filetype specific" classes work. But in general, if you create a tree as data structure, you'll have some work to maintain the nodes and to traverse.
Why not simply creating a List
, an ArrayList
or a Collection
and Hashtable
elements like this:
ArrayList directoryInformation = new ArrayList();
...
...
Hashtable fileEntry = new Hashtable();
fileEntry.add("name", theFileName);
fileEntry.add("type", theFileType);
fileEntry.add("path", theFilePath);
...
...
directoryInformation.add (fileEntry);
Once the list is built, your classes can simply loop through the list and get the information needed from the Hashtable
entries. Further by adding the path to each element, you flatten the tree structure so you'll have to act recursively only once.
The only disadvantage of the Hashtable
s is that it is not typed, so the compiler won't cough if you add some wrong strings. But it returns value by being flexible and fast to write.
Upvotes: 1