Răspuns Determine correct sub-directory path

  • Wednesday, March 07, 2012 12:23 AM
     
     

    I am browsing through the Windows file system using the following code:

    subDirs = System.IO.Directory.GetDirectories(sDirectoryPath);

    subDirs1 = System.IO.Directory.GetDirectories(subDirs[0]);

    subDirs2 = System.IO.Directory.GetDirectories(subDirs[1]);

    This works fine in principal but stops working when I am looking at a special directory such as My Pictures.

    I am using a German Windows 7 version and my C# program runs in EN-en culture. And when I retrieve the sub-directories of the My Pictures folder (in German: "C:\Users\MyUser\Documents\Eigene Bilder") I am getting:

    System.UnauthorizedAccessException was caught
      Message=Access to the path 'C:\Users\Admin\Documents\Eigene Bilder\' is denied.

    and whats really odd, my real physical path to that directory is: "C:\Users\MyUser\Pictures"

    So, it seems like I must have confused physical and logical path at some point. Stepped into a logical (virtual?) folder without realizing it.

    How can I browse the sub-folders of the my documents folder without having to worry about this problem? Do I have to set the correct UI culture to get this to work correctly? Is there a method that works on Windows XP, VISTA, and Windows 7?

All Replies

  • Wednesday, March 07, 2012 12:26 AM
     
     
  • Wednesday, March 07, 2012 1:36 AM
     
     

    Hi Dirkster9999,

      where is your execute file stored in? It seems that you need to make sure your exe file is locate at the correct position.If not, you need to assign the full path to GetDirectories method. Furthermore, you can use the Directory.GetCurrentDirectory  to get the current directory ,its official document as follows:

    http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory.aspx 

      Sincerely,

      Jason Wang


    orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Wednesday, March 07, 2012 8:01 AM
     
     

    Hi JP,

    I am aware of this enumeration but it does not solve my problem. My problem is that I am browsing the file system and I am getting into a special folder without being aware of it. I would have to match the current path with each enumeration - each time I am retrieving sub-dirs to avoid this. Surely, there is a better solution, right?

  • Wednesday, March 07, 2012 8:04 AM
     
     

    Hi Jason,

    I am using the Full Path (eg.: 'C:\TEMP\') each time I retrieve sub-dirs - so, I do not think this is the problem but I will debug the code to see whether something springs up. As far as the location of my exe goes: it should not make a difference at all - why do you think it is important?

  • Wednesday, March 07, 2012 8:28 AM
     
      Has Code

    you  have  a privileges issue you  must elevate  your process  access  level  . to  do  this you  should  add app  manifest with 

     <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
          <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
           
            <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
          
          </requestedPrivileges>
        </security>
      </trustInfo>

    if you need  more information  on  how windows vista implement UAC please follow  this  link http://msdn.microsoft.com/en-us/library/aa905330.aspx 

    hope this  help  .......!!!


    A man's dreams are an index to his greatness

  • Wednesday, March 07, 2012 8:43 AM
     
     Proposed Answer

    Hi Dirkster9999,
    The problem is your process is running as the current user, but trying to gain access to a folder that is owned by a different user. This throws an UnauthorizedException because the current user is not the owner, or does not have security permissions to access the other users folder. This is expected behavior and is not a bug, and not a reason to elevate your process either, so don't do that. Elevating your process is not the answer.

    A few steps. First make sure you debug the code by stepping through it line-by-line. Set a breakpoint or run-to-cursor on

        subDirs = System.IO.Directory.GetDirectories(sDirectoryPath);

    Then step through the next two lines making sure that your path is actually set to

        C:\Users\MyUser\Documents\Eigene Bilder

    The only time you might get an UnauthorizedException is if you are enumerating files and folders in the C:\Users\Public\Documents. So validate that you are actually looking in the right path.

    Culture and OS version won't cause this exception, though you really should be using Environment.SpecialFolder.MyDocuments and that will save you a lot of headaches. Never hard code paths or assume paths.

    You should also wrap GetDirectories in a try-catch, because while you could check directory ACCL attributes and permissions, its not guarunteed to prove fail-safe, so make sure you do

        try {
            subDirs = System.IO.Directory.GetDirectories(sDirectoryPath);
            ...
        }
        catch (System.UnauthorizedAccessException ex) {
            // Handle exception

        }

    You also must know that there are files that you simply just won't have access to, even as Administrator. Even if you really need to elevate your process to do what you need to, it still is not a guaruntee for all files and folders. Some examples are System Volume Information. There is no resolution for folders like that unless you use a lower level API, or WMI maybe.

    Hope this answers your questions.


    Currently developing FaultTrack. I occassionally blog about C# and .NET.
    Hoping to become a MVP by 2013. Email: danderson [at] dcomproductions [dot] com


    • Edited by Dave_Anderson Wednesday, March 07, 2012 8:47 AM
    • Proposed As Answer by Mr. Javaman II Wednesday, March 07, 2012 1:53 PM
    • Unproposed As Answer by Dirkster9999 Wednesday, March 07, 2012 6:09 PM
    • Proposed As Answer by Mr. Javaman II Friday, March 09, 2012 2:07 PM
    •  
  • Wednesday, March 07, 2012 6:09 PM
     
     

    Hi Dave,

    I tried executing my program as Administrator but that only produced the same exception as described above (which I was able to catch because I surrounded System.IO call with a try catch block):

    Next, I'll be looking at a more detailed debug output and get back here once I have more evidence. Your observation are correct but I think it should be possible to retrieve all sub-dirs in a user directory without hitting a permission problem - especially when being an admin or even looking into the own folder. Sorry, but this far the answer to the problem is not on the table :(

  • Wednesday, March 07, 2012 7:53 PM
     
     

    I have looked more closely (with a fresh mind) and it appears that I am following a link:

    and my program seems to complain because the target is none existing. I am getting the same in Windows Explorer. Strange. So this is now off the C# topic. Is it possible that Windows 7 cannot distinguish between a link target that does not exists and a link target that leads to a target with insufficient user permissions?

    I did not know that links are now viewed as sub-dirs. Or could this be hard-links and (I created a new shortcut and it does not show as subdir) they are in fact pointing to an invalid location?

  • Wednesday, March 07, 2012 9:24 PM
     
     Answered

    Just because your running your program as an administrator does not guaruntee access to any one folder. You need to either login as the user that owns the folder and change its permissions, or you just can't have access to it at all. There isn't a resolution for that, because this is the way security permissions were designed to work. The system isn't going to let you get into something you don't have permission to. I thought I was pretty clear on these key points.

    If you login to the owner account for that  folder and check the permissions, I can bet that you will find that even the Administrators group may not have access to it. Otherwise there may be some flaw in the ACCL for that folder.

    I think maybe you might want to read up on file and folder security permissions for your endeavour so you have a better understanding of the problem. From your most recent post with the pictures of the directories, you should be getting an UnauthorizedException because you are accessing someone elses folder that you don't have permission to.

    Also note that shortcuts are .lnk files, not actual folders which is why you don't find them while enumerating sub directories.

    Hope this helped.


    Currently developing FaultTrack. I occassionally blog about C# and .NET.
    Hoping to become a MVP by 2013. Email: danderson [at] dcomproductions [dot] com


    • Edited by Dave_Anderson Wednesday, March 07, 2012 9:43 PM
    • Proposed As Answer by Mr. Javaman II Friday, March 09, 2012 2:07 PM
    • Marked As Answer by Dirkster9999 Saturday, March 10, 2012 11:15 PM
    •  
  • Wednesday, March 07, 2012 9:51 PM
     
     

    Hey Dave,

    you've been clear alright but the thing is that access permissions may not be the full story. To me this looks like dead hard-links(?) but I cannot proof it. The thing is, I meanwhile found about 20 more these and it looks like someone who has setup my computer in the shop has (I recently got this one new) mixed the German and English setups and thats why these things look so confusing.

    I'd like to clean this so I tried deleting one of them and that works (what about permissions now?). Now I cannot tell whether the hard-link(?) points to a valid directory or not. So, I'd like to follow your advice and login as the owner of that thing. But how do I determine the owner? There is currently only one user login on this computer so I bet you that we are looking at a dead link that is treated like a permission problem. But I am willing to accept other options...

  • Thursday, March 08, 2012 2:05 AM
     
     Proposed Answer

    If you can open it, its valid. If you can't, right click and see what it points to then try to manually go to that path. It may still exist, but you may get a security or permissions error, in which case the path is still valid, just your account isn't allowed.

    1. Right click a folder or file
    2. Click `Properties`
    3. Click the `Security` tab

    Here do your check to make sure that the user account you are running as has permission on the file or folder for `Groups or User names`. If it does not, the account you are running doesn't have permission.

    4. Next, click `Advanced`.
    5. Now click the `Owner` tab

    This will display the user account that owns the folder. If you are running as Administrator you should be able to change the owner. Sometimes things can get bad if someone changes security permissions, or does something funky to your computer. It may sound like you have a bunch of messed up ACCL's for your files and folders. Usually its just easier to reformat to a clean system then spend countless hours manually repairing permissions.


    Currently developing FaultTrack. I occassionally blog about C# and .NET.
    Hoping to become a MVP by 2013. Email: danderson [at] dcomproductions [dot] com

    • Proposed As Answer by Mr. Javaman II Friday, March 09, 2012 2:07 PM
    •  
  • Friday, March 09, 2012 5:24 AM
    Moderator
     
     

    Hi Dirkster,

    Welcome to the MSDN forum!

    How is it going with the problem currently?

    You may check the properties of the shortcut file to see the target location of it. 

    Let us know if you need any help.

    Have a nice day.

    yoyo


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

  • Friday, March 09, 2012 1:19 PM
     
     

    Hi Yoyo,

    thanks for your interest but please allow a few days as I am not allways working on the same computer :(

    But I will get back her soon.

    Cheers Dirkster