Hello everybody
I have wrote a recursive function in VS2008 with C# Language to scan All folders in a Specific drive.
i used System.Io Functions .
it's here:
private void scan(string DriveName)
{
string[] Directories = Directory.GetDirectories(DriveName);
foreach (string foldername in Directories)
{
scan(foldername); // recursive call
listBox1.Items.Add(foldername); // Add Everything To My List
}
}
but there is a problem .when i scan a cd contained a folder with invalid name like "$#%$#^|b a b a k %&%&#$#@||" , It encounter problem and scan failed .The message is "Illegal characters in path exception" but I can do that scan by vb6 useing COM Dlls .how can i solve it and scan All file and Folders with illegal names by useing dot net framework classes & functions !
send me your idea please : [email protected]
thank you very much in advanced.
Regards
Sam HobbsPosted Jun 5, 2011, 10:19 PM
Also, if it were me, I sure would look at the Directory class very well; there might be an alternative that would allow you to process the folder names that are causing you problems. For example, since performance is important, the documentation says to use the Directory.EnumerateDirectories Method instead of GetDirectories for better performance.
IRDump IRDumpPosted Jun 5, 2011, 3:46 PM
hi, thanks for answering .
Speed is very important for me .
imagine that we have a folder with illegal name & 1GigaByte size;\
in Your Solution We Have a lot of Squandered Time for copying it to another place!
how we can do it faster?
thank you 4 every thing .
Bryian TanPosted Jun 5, 2011, 9:30 AM
I agreed with Sam, check if the folder is valid before calling the scan. If the folder contains illegal character, copy it to a temp folder with a different name and scan it.
Sam HobbsPosted Jun 5, 2011, 3:55 AM
Alternatively put the GetDirectories in a "try" block with a corresponding "catch" block. Developers often don't use try/catch enough.