Click here to Skip to main content
15,850,685 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi I 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 I have a Problem whit it .when i want to scan a drive that have a folder with "$#%$#^|b a b a k %&%&#$#@||" name
I encounter to a exception .that's "Illegal characters in path exception". how can i solve it?
i want to scan All Folders with illegal names.i only want to use dot net framework classes & functions !

please , send me your idea : [Email removed]
thank you very much .
Updated 4-Jun-11 16:12pm
Monjurul Habib 4-Jun-11 17:38pm    
Edited: code block.
Christian Graus 4-Jun-11 20:35pm    
Posting your email address is stupid. Web bots scan the web for people that stupid, and put them on spam lists.
Philippe Mori 4-Jun-11 22:13pm    
In fact, from the sample name above, only the | is an invalid character in a path. If you want to access such directory, then rename it to something valid.

Invalid characters as reported by Windows Explorer are \/:*?"<>|. All those characters have a special meaning at the command prompt.

Well, you've got a problem with that folder. There are indeed illegal characters in the folder name.

For the short term, wrap your call to GetDirectories in a Try/Catch block and report the error to the user. You won't be able to get all of the subfolder because of the error. You cannot navigate into a folder that uses illegal characters in its name. The .NET classes will not work at all on these folders.

Your real question should be WHY you have these folders laying around in the first place. I suggest scanning your machine for viruses and malware before continuing.
Share this answer

Before sending Directory values in to the String array Encrypt the Directory Names and pass it as byte[] array and than scan for the names.

Sample Code For Encrypt:

private byte[] encodeProcess(string DriveName)
    int[] AddInt = new int[DriveName.Length];
    byte[] AddByte = new byte[DriveName.Length];
    for (int i = 0; i < DriveName.Length; i++)
        AddInt[i] = (int)DriveName[i];
    for (int j = 0; j < DriveName.Length; j++)
        AddByte[j] = (byte)AddInt[j];
    return AddByte;

Note: The characters are encoded in to UTF-8 format and than use it.

Use This Link It Will Be Helpful:
Share this answer
IRDump 9-Jun-11 16:42pm    
how can i use it ?
i've problem with "Directory.GetDirectories(DriveName);"
before processing use some validation like......
private void scan(string path)
    if (Directory.Exists(path))
        DirectoryInfo di = new DirectoryInfo(path);
        DirectoryInfo[] Directories = di.GetDirectories("*",SearchOption.AllDirectories);

        foreach (var item in Directories)
            listBox1.Items.Add(item.Name);// Add Everything To My List
            foreach (var fileItem in item.GetFiles())
                listBox1.Items.Add("   " + fileItem.Name);//Add Everything To My List
            scan(item.FullName);// recursive call
Share this answer
IRDump 9-Jun-11 16:37pm    
that's not worked .
i steal recieve that exception .
thank you for everthing
thanks for answering .
I can do it in vb6 By Using COM(Activex DLL) & (.Net Interop service).
but I want to do that with using .netframework functions.

thanks a lot .
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900