CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2007
    Location
    .NET 3.5
    Posts
    38

    [RESOLVED] Access to the path 'D:\System Volume Information' is denied

    Hi... I'm new to programming, and trying to learn C#, so please be very explicit when replying. I'm trying to build an TreeView Explorer app that will list directories and files anywhere on my computer. The code below works great when listing directories and files on a CDROM Drive, but when I change the path to:
    DirectoryInfo info = new DirectoryInfo(@"d:");

    I get this exception:
    "An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
    Additional information: Access to the path 'D:\System Volume Information' is denied."

    The program breaks on this line:
    subSubDirs = subDir.GetDirectories();

    I've searched everywhere, and even found a solution suggesting to wrap my code in a Try catch block, but I don't understand how to do this. Would be so grateful if someone could explain the solution and show how to implement it. Thanks much...

    Here's the code:

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication3
    {
    public partial class Form1 : Form
    {

    public Form1()
    {
    InitializeComponent();
    PopulateTreeView();
    }


    private void PopulateTreeView()

    {
    TreeNode rootNode;

    DirectoryInfo info = new DirectoryInfo(@"d:");
    if (info.Exists)
    {
    rootNode = new TreeNode(info.Name);
    rootNode.Tag = info;
    GetDirectories(info.GetDirectories(), rootNode);
    treeView1.Nodes.Add(rootNode);
    }
    }

    private void GetDirectories(DirectoryInfo[] subDirs,
    TreeNode nodeToAddTo)
    {
    TreeNode aNode;
    DirectoryInfo[] subSubDirs;
    foreach (DirectoryInfo subDir in subDirs)
    {
    aNode = new TreeNode(subDir.Name, 0, 0);
    aNode.Tag = subDir;
    aNode.ImageKey = "folder";
    subSubDirs = subDir.GetDirectories();
    if (subSubDirs.Length != 0)
    {
    GetDirectories(subSubDirs, aNode);
    }

    nodeToAddTo.Nodes.Add(aNode);
    }

    }

    void treeView1_NodeMouseClick(object sender,
    TreeNodeMouseClickEventArgs e)
    {
    TreeNode newSelected = e.Node;
    listView1.Items.Clear();
    DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
    ListViewItem.ListViewSubItem[] subItems;
    ListViewItem item = null;

    foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
    {
    item = new ListViewItem(dir.Name, 0);
    subItems = new ListViewItem.ListViewSubItem[]
    {new ListViewItem.ListViewSubItem(item, "Directory"),
    new ListViewItem.ListViewSubItem(item,
    dir.LastAccessTime.ToShortDateString())};
    item.SubItems.AddRange(subItems);
    listView1.Items.Add(item);
    }
    foreach (FileInfo file in nodeDirInfo.GetFiles())
    {
    item = new ListViewItem(file.Name, 1);
    subItems = new ListViewItem.ListViewSubItem[]
    { new ListViewItem.ListViewSubItem(item, "File"),
    new ListViewItem.ListViewSubItem(item,
    file.LastAccessTime.ToShortDateString())};

    item.SubItems.AddRange(subItems);
    listView1.Items.Add(item);
    }

    listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
    }

    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {

    }

    }
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Access to the path 'D:\System Volume Information' is denied

    Please use CODE TAGS so we can read your code.
    Additional information: Access to the path 'D:\System Volume Information' is denied."
    Probably a post Windows XP system, which has VIRTUAL FOLDERS, not a tree-view (as you've found out)

    Code:
    ' code goes here
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Access to the path 'D:\System Volume Information' is denied

    Here's how you write a try/catch block:

    http://msdn.microsoft.com/en-us/library/0yd65esw.aspx

    wrap the following line, in the try/catch.

    Code:
    subSubDirs = subDir.GetDirectories();

  4. #4
    Join Date
    Sep 2010
    Posts
    3

    Re: Access to the path 'D:\System Volume Information' is denied

    //DirectoryInfo info = new DirectoryInfo(@"d:");

    DirectoryInfo info = new DirectoryInfo(@"d:\\");

    the path u have specified is invalid... u forgot the \
    u need to 2 "\" eg: "c:\\" to escape the \
    if path is invalid it goes to that system... dir.. by default

    good luck

  5. #5
    Join Date
    Jan 2007
    Location
    .NET 3.5
    Posts
    38

    Re: Access to the path 'D:\System Volume Information' is denied

    Arjay, it WORKED! I can't describe how frustrating, and at the same time gratifying it is when I learn a 3 minute solution to something I have agonized over for 3 days... but I know I don't have to explain that to anyone here.

    Thanks, so much for the assistance, guys. I'm still new to the forums so I didn't know about the WYSIWYG editor. Will use it next time I post code... Thanks again!

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Access to the path 'D:\System Volume Information' is denied

    Quote Originally Posted by n*****09 View Post
    //DirectoryInfo info = new DirectoryInfo(@"d:");

    DirectoryInfo info = new DirectoryInfo(@"d:\\");

    the path u have specified is invalid... u forgot the \
    u need to 2 "\" eg: "c:\\" to escape the \
    if path is invalid it goes to that system... dir.. by default

    good luck
    No, you don't. The @ symbol signifies a verbatim string.

  7. #7
    Join Date
    Jun 2006
    Location
    Chile
    Posts
    13

    Re: [RESOLVED] Access to the path 'D:\System Volume Information' is denied

    I have a similart problem while looping through directories to get files, why does the problem go away after inserting a try/catch ? it doesn't work in my case, I keep getting "access to C:\\system volume information is denied". I use windows XP and I can't open that folder even when I log in as an administrator. What is it ?
    Pumpobee is prolounced as mumbolee

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [RESOLVED] Access to the path 'D:\System Volume Information' is denied

    Quote Originally Posted by PumpoBee View Post
    I have a similart problem while looping through directories to get files, why does the problem go away after inserting a try/catch ? it doesn't work in my case, I keep getting "access to C:\\system volume information is denied". I use windows XP and I can't open that folder even when I log in as an administrator. What is it ?
    Post the code you are using. It's hard to see any code problems from only a description.

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [RESOLVED] Access to the path 'D:\System Volume Information' is denied

    My guess would be that in the catch block he is showing the error message, or the call is outside the try block.
    Always use [code][/code] tags when posting code.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured