我试图将目录列表添加到TreeView系统,但我遇到了似乎是用户访问问题的问题。我已经尝试了解决这个问题的各种步骤,其中没有一个能够奏效。其中包括:更改解决方案清单文件中的安全性,使用try catch跳过我无法访问的文件,并更改我的Windows用户文件夹设置以完成控制(管理员)。我在网络中寻找了类似问题的答案,大多数人都使用了try catch系统。这对我的系统不起作用,因为所有东西都冻结了,它就坐在那里。然后,程序就像在我的整个计算机上没有找到单个目录一样。我的代码包括:尝试将目录添加到TreeView时发生UnauthorizedAccessException
public Folder_Browser()
{
InitializeComponent();
MapDirectory();
}
private void MapDirectory()
{
TreeNode rootNode;
DirectoryInfo dirPrograms = new DirectoryInfo(@"/");
DriveInfo[] loadedDrives = DriveInfo.GetDrives();
foreach (DriveInfo dr in loadedDrives)
{
if (dr.DriveType != DriveType.Removable)
{
DirectoryInfo info = new DirectoryInfo(dr.Name);
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";
try
{
subSubDirs = subDir.GetDirectories();
//ERROR HERE^^^^^^^
if (subSubDirs != null && subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
catch (System.UnauthorizedAccessException)
{
}
}
}
每次我试图实现别人的解决这种问题,我只是没有得到任何形式的目录列表中走出来。该程序占用了太多的资源,忽略了它无法触及的文件夹。有没有简单的我忽略了?或者这种方法是不可能的?任何帮助表示赞赏,干杯。