2

I made a program tat should display a filesystem tree. I set it to display the filesystem from C:. When I compile the program says that access is denied for C:. Tell me what would you need in case you would help me and I will provide you the required information. Thanks!

P.S. When I set the program to list the filesystem in C:\Windows\ it worked.

This is the code that I used:

private void ListDirectory(TreeView treeView, string path)
{
    treeView.Nodes.Clear();
    var rootDirectoryInfo = new DirectoryInfo(path);
    treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
}

private static TreeNodeCreateDirectoryNode(DirectoryInfo directoryInfo)
{
    var directoryNode = new TreeNode(directoryInfo.Name);
    foreach (var directory in directoryInfo.GetDirectories())
        directoryNode.Nodes.Add(CreateDirectoryNode(directory));
    foreach (var file in directoryInfo.GetFiles())
        directoryNode.Nodes.Add(new TreeNode(file.Name));
    return directoryNode;
}

In the program, ti call the method I used:

mainWindow(){
    InitialiseComponent();
    ListDirectory(treeView1, @"C:\");
}
  • 2
    Can you post the relevant parts of your code? – Frédéric Hamidi Sep 12 '12 at 20:10
  • what version of windows? What version of VS? Have you tried running as admin? – Jason Sep 12 '12 at 20:10
  • 2
    Does running it with Admin Priviledges (right click output exe, Run as Admin) work? – Corey Ogburn Sep 12 '12 at 20:11
  • Does the user running the application have access to C:\? When the user navigates to that folder in Windows Explorer, does it show the files or does it prompt the user in some way to ask if it should show the files? – David Sep 12 '12 at 20:11
  • 4
    Not sure why you locked it so fast. Maybe give him time to add/improve his question next time. – IAmGroot Sep 12 '12 at 20:12
  • 3
    @Guys, if you need admin privileges to *read* the root of the drive, you will have serious problems accessing the rest of the filesystem. Just saying. – Frédéric Hamidi Sep 12 '12 at 20:12
  • I will post here the function used to populate the treeview. And I am using Windows 7 Ultimate 64bit, Visual Studio 2010, and YES, I tried to run it as admin. Nothing worked. –  Sep 12 '12 at 20:12
  • Ok. If the users are bad here and close qiestions before asking me to improve it, I will post the question somwere else where the users are able to listen something until its end. –  Sep 12 '12 at 20:23
  • @Victor, for what it's worth, closing a question is not the same as locking it. You can still edit it to include your code, and it will probably be reopened when you do that (there already are two reopen votes pending). – Frédéric Hamidi Sep 12 '12 at 20:24
  • Thank you! But I already added the example that I used. I think you could browse its code... –  Sep 12 '12 at 20:27
  • @Victor, you would be wrong. It's not because I'm too lazy to browse that code (alright, it's *partly* because I'm lazy), but mostly because Stack Overflow aims to be self-contained. Posting the relevant code in your question both makes it easier for people to understand your problem and guarantees the code will remain available in the (pretty remote, I concede) event `code.msdn.microsoft.com` goes down or removes that code in the future. – Frédéric Hamidi Sep 12 '12 at 20:30
  • excuse me! I don't understand what you mean @CodesInChaos –  Sep 12 '12 at 21:03
  • 1
    Markdown tricked me. Are you using `C:` or ``C:\``? – CodesInChaos Sep 12 '12 at 21:07
  • Now you can see all the code, both declaring and calling of the functions. –  Sep 12 '12 at 21:11

1 Answers1

2

This code will run under the user account that is executing it. Based on the permissions of that account a System.UnauthorizedAccessException may occur for some directories such as a user account folder or the recycle bin.

This will not prevent you from navigating partway through the directory structure, but would prevent that account from reading all of the directories inside of the protected folders.

You could write code to pull the access control list using directoryInfo.GetAccessControl()

Or you could catch the System.UnauthorizedAccessException. Then your code might look like this:

try
{
    var directoryNode = new TreeNode( directoryInfo.Name );
    foreach ( var directory in directoryInfo.GetDirectories() )
        directoryNode.Nodes.Add( CreateDirectoryNode( directory ) );
    foreach ( var file in directoryInfo.GetFiles() )
        directoryNode.Nodes.Add( new TreeNode( file.Name ) );
    return directoryNode;
}
catch ( System.UnauthorizedAccessException )
{
    return new TreeNode( "Unavailable Node" );
}
catch ( System.IO.PathTooLongException )
{
    return new TreeNode( "Unavailable Node" );
}
Community
  • 1
  • 1
Matthew K
  • 973
  • 6
  • 21