-
September 20th, 2010, 02:54 PM
#1
[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)
{
}
}
}
-
September 20th, 2010, 05:19 PM
#2
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)
-
September 20th, 2010, 05:22 PM
#3
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();
-
September 20th, 2010, 05:25 PM
#4
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
-
September 20th, 2010, 08:08 PM
#5
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!
-
September 21st, 2010, 12:53 AM
#6
Re: Access to the path 'D:\System Volume Information' is denied
Originally Posted by n*****09
//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.
-
October 7th, 2011, 02:47 PM
#7
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
-
October 7th, 2011, 03:50 PM
#8
Re: [RESOLVED] Access to the path 'D:\System Volume Information' is denied
Originally Posted by PumpoBee
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.
-
October 8th, 2011, 01:01 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|