Hello,
I have developped a tool using c#.net (windows application) that searches all files under subdirectories and get the files attributes (creation date, author, size, type...), and then it displays the data in a richtexteditor object...
my tool works fine: it takes few seconds for hundreds of files. but when testing thousands of files it takes hours, like for 10 000 files it takes arround 12 hours, for 30 000 files it takes more than 40 hours
is there a way you can advice to enhance my code structure? do i need to use threads? or anything like.. i know the application is consuming lot of memory while running
thanks for any advice
Loading
Mahesh ChandPosted Apr 1, 2011, 12:27 PM
Put a try..catch..finally and Dispose oDetailedFileInfo inside the finally block.
AbdPosted Apr 1, 2011, 10:45 AM
I was rechecking my code, and i assume i found where the issue is.
In my code i am using
CFileInfo oDetailedFileInfo = new CFileInfo(fi);
which is creating an instance of this class each time the function is being called, and this is a recursive function... so for sure it is consuming lot of memory
so i guess the question is how to manage the memory here?
Ibrahim ErsoyPosted Feb 27, 2011, 10:39 AM
You must absolutely use Parallel Programming case here.The links below will help you understand it.
http://aditiblogs.com/blog/blog/2009/06/07/parallel-programming-in-c-40-using-visual-studio-2010/
http://www.albahari.com/threading/part5.aspx
http://blogs.msdn.com/b/pfxteam/archive/2009/06/30/9809774.aspx
Good Luck!
AbdPosted Feb 27, 2011, 8:13 AM
i attached the helper class file
and the following is the function am using in the main:
private void ProcessDir(string sourceDir)
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fi in fileEntries)
{
try
{
// Read File Details from CFileInfo Object
CFileInfo oDetailedFileInfo = new CFileInfo(fi);
rtbResult.Text += oDetailedFileInfo.FileName + ";" + oDetailedFileInfo.FileAuthor + ";" + oDetailedFileInfo.FilePath + ";" + oDetailedFileInfo.FileCreationDate + ";" + oDetailedFileInfo.FileCreationDate2 + Environment.NewLine;
}
catch (Exception ux)
{
Log(Application.StartupPath + "\\Log.txt", DateTime.Now + Environment.NewLine + "Exception Error: " + ux.Message + Environment.NewLine + Environment.NewLine);
continue;
}
}
// Recurse into subdirectories of this directory.
string[] subdirEntries = Directory.GetDirectories(sourceDir);
foreach (string subdir in subdirEntries)
if ((File.GetAttributes(subdir) & FileAttributes.ReparsePoint) !=FileAttributes.ReparsePoint)
ProcessDir(subdir);
}
Felipe RamosPosted Feb 27, 2011, 7:55 AM