Results 1 to 8 of 8

Thread: Why does this coding fail if drive directory chosen?

  1. #1
    tommyt11 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2015
    Location
    Columbus, Ohio
    Posts
    23
    Reputation
    17

    Question Why does this coding fail if drive directory chosen?

    Hi, I'm confused. This code works fine as long as directory chosen from directory dialog is not a root directory on any drive. Example if I choose C:/ or F:/ it fails and I get exception:
    'System.UnauthorizedAccessException' occurred in mscorlib.dll.
    Is there a way to handle this exception and continue the process on folders not readonly or set permissions so I can.

    If I choose a subdirectory. example F:/Something it works as I assume there is no permissions problems at the lower level.
    Here is code:
    PublicSub GetFilesForTransfer()

    OnErrorResumeNext
    Dim SelectedsearchPath = SearchPathTextBox.Text
    Dim searchMask = MaskComboBoxMain.Text
    If SubFoldersYes.Checked Then
    ForEach foundFile AsStringInMy.Computer.FileSystem.GetFiles(SelectedsearchPath, FileIO.SearchOption.SearchAllSubDirectories, searchMask)
    SearchResultsListBox.Items.Add(foundFile)
    Next
    Else

    ForEach foundFile AsStringInMy.Computer.FileSystem.GetFiles(SelectedsearchPath, FileIO.SearchOption.SearchTopLevelOnly, searchMask)
    SearchResultsListBox.Items.Add(foundFile)
    Next
    EndIf

    OnErrorResumeNext

    EndSub

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,776
    Reputation
    1703
    The GetFiles method will fail if you specify SearchAllSubDirectories and the folder you specify contains an inaccessible subfolder somewhere, which will definitely be the case on a system drive at least. If you want to be able to handle that scenario then you'll have to write your own recursive file search.

  3. #3
    tommyt11 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2015
    Location
    Columbus, Ohio
    Posts
    23
    Reputation
    17
    OK Thanks for the response, I can do that.

  4. #4
    tommyt11 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2015
    Location
    Columbus, Ohio
    Posts
    23
    Reputation
    17
    Okay Let's say I just want to prohibit user from this scenario and I go this route,If
    SearchPathTextBox.Text = ""

    but display a prompt that this can not be done.
    how do I wild card Search text so they can't use any root directory like
    SearchPathTextBox.Text = "?:" which of course doesn't work.
    I can do an OR but it's tedious, there has to be a way to wild card this I would think>






  5. #5
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,776
    Reputation
    1703
    You don't need to use a wildcard. The Path class can give you the root path from another path and you can then just test whether they are the same.

  6. #6
    tommyt11 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2015
    Location
    Columbus, Ohio
    Posts
    23
    Reputation
    17
    But how would I handle that simply with out a boatload of if's. I was trying to avoid a long string of if or's. Maybe I'm not understanding. Is there an example of what you're talking about .

  7. #7
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,776
    Reputation
    1703
    You need one If: if the path entered is equal to the root of the path entered.

  8. #8
    tommyt11 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2015
    Location
    Columbus, Ohio
    Posts
    23
    Reputation
    17
    Okay, now I get it. Thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •