Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm looking for a file in my computer.
Using this way

For Each foundFile As String In My.Computer.FileSystem.GetFiles(
	My.Computer.FileSystem.SpecialDirectories.MyDocuments,
        Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, 
        UploadControl.FileName)
	fullPath.Add(foundFile)
    Next


But in some subdirectories access does not allow throwing error `access is denied`

I do not want to manage the error but go to the next subdirectory.

How can I do this ?

What I have tried:

Be very careful here
What I want is: When the problem occurs the code has to leave it and got to another directory.
Posted
Updated 27-Jan-19 6:56am

You have to enumerate the directories and files yourself. You're not going to be able to use the My.Computer shortcut for this.

See Directory.GetDirectories[^] and Directory.GetFiles[^].

You're going to have two loops, an outer directory loop and an inner files loops. Wrap the inner files loop in a try/catch block and you'll be able to catch the exception and still continue with the next directory.
 
Share this answer
 
Comments
Lefteris Gkinis 27-Jan-19 15:12pm    
Your'e grate... I found my solution Yes... thanks a lot
To add to what Dave says, there are folders in your documents that the system maintains: mostly concerned with the recycle bin.
If you check for them, you can reduce the number of exceptions, which will save time (significant time if your documents folder has a lot of subdirectories).
This may help:
C#
private long SizeDirFolders(string dir, DirNode node)
    {
    if (!IsIgnorable(dir))
        {
        if (dir.EndsWith(":")) dir += "\\";
        foreach (string path in Directory.GetDirectories(dir))
            {
            try
                {
                int last = path.LastIndexOf('\\');
                if (last < 0)
                    {
                    last = 0;
                    }
                string sname = path.Substring(last);
                if (!IsIgnorable(sname))
                    {
                    DirNode dn = new DirNode(sname, 0);
                    lock (Root)
                        {
                        node.Add(dn);
                        }
                    SizeDir(path, dn);
                    }
                }
            catch (UnauthorizedAccessException) { } //Discard
            catch (Exception ex)
                {
                DialogResult result =  MessageBox.Show("An error occurred: " + ex.Message + "\nResults for the " + dir + "will not be complete", "Error!", MessageBoxButtons.OKCancel);
                if (result == DialogResult.Cancel) return node.Size;
                }
            }
        }
    return node.Size;
    }

private bool IsIgnorable(string dir)
    {
    if (dir.EndsWith(":System Volume Information")) return true;
    if (dir.Contains(":$RECYCLE.BIN")) return true;
    return false;
    }

private void SizeDir(string dir, DirNode node)
    {
    if (!IsIgnorable(dir))
        {
        DirNode dn = SizeDirFiles(dir);
        lock (Root)
            {
            node.Add(dn);
            }
        SizeDirFolders(dir, node);
        }
    }
 
Share this answer
 
Comments
Lefteris Gkinis 27-Jan-19 14:08pm    
Yes I see, but you have something in your code that I can't understand.
The issue is in `SizeDirFiles(dir)` which is probably some function which is not declared.
Can you help on that ?
You see I turn it all to vb.net and there is hang.
Lefteris Gkinis 27-Jan-19 14:28pm    
Also I can't figured out how this class is working. How to send the file and return the subdirectory path
Lefteris Gkinis 27-Jan-19 15:12pm    
Your'e grate... I found my solution Yes... thanks a lot both your'e grate thanks
OriginalGriff 28-Jan-19 1:36am    
You're welcome!

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