Directory listing
-
Hello, I'm building an upload tool in c# but I only want to show de directories where the user has access to from the computer he is sitting on. I made a script that lists al the directories in a treeview, but it gives an error when it comes to a directory where the user had no access to. (ex. c:/documents and setting/administrator) Here a sample of my script private void FillDirectoryTree(TreeView tvw) { tvw.Nodes.Clear(); string[] strDrives = Environment.GetLogicalDrives(); foreach (string rootDirName in strDrives) { MessageBox.Show(rootDirName); if(rootDirName == "C:\\") { try { DirectoryInfo dir = new DirectoryInfo(rootDirName); dir.GetDirectories(); TreeNode ndRoot = new TreeNode(rootDirName); tvw.Nodes.Add(ndRoot); GetSubDirectoryNodes(ndRoot, ndRoot.Text, 1); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } Application.DoEvents(); } private void GetSubDirectoryNodes(TreeNode parentNode, string fullName, int level) { DirectoryInfo dir = new DirectoryInfo(fullName); DirectoryInfo[] dirSubs = dir.GetDirectories(); foreach (DirectoryInfo dirSub in dirSubs) { if ((dirSub.Attributes & FileAttributes.Hidden) != 0) { continue; } TreeNode subNode = new TreeNode(dirSub.Name); parentNode.Nodes.Add(subNode); if (level < MaxLevel) { GetSubDirectoryNodes(subNode, dirSub.FullName, level + 1); } } } private void tvDestFiles_AfterSelect(object sender, TreeViewEventArgs e) { txtTargetDir.Text = tvDestFiles.SelectedNode.FullPath; } private void btnUploadFiles_Click(object sender, EventArgs e) { List fileList = GetFileList(); foreach (FileInfo file in fileList) {
-
Hello, I'm building an upload tool in c# but I only want to show de directories where the user has access to from the computer he is sitting on. I made a script that lists al the directories in a treeview, but it gives an error when it comes to a directory where the user had no access to. (ex. c:/documents and setting/administrator) Here a sample of my script private void FillDirectoryTree(TreeView tvw) { tvw.Nodes.Clear(); string[] strDrives = Environment.GetLogicalDrives(); foreach (string rootDirName in strDrives) { MessageBox.Show(rootDirName); if(rootDirName == "C:\\") { try { DirectoryInfo dir = new DirectoryInfo(rootDirName); dir.GetDirectories(); TreeNode ndRoot = new TreeNode(rootDirName); tvw.Nodes.Add(ndRoot); GetSubDirectoryNodes(ndRoot, ndRoot.Text, 1); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } Application.DoEvents(); } private void GetSubDirectoryNodes(TreeNode parentNode, string fullName, int level) { DirectoryInfo dir = new DirectoryInfo(fullName); DirectoryInfo[] dirSubs = dir.GetDirectories(); foreach (DirectoryInfo dirSub in dirSubs) { if ((dirSub.Attributes & FileAttributes.Hidden) != 0) { continue; } TreeNode subNode = new TreeNode(dirSub.Name); parentNode.Nodes.Add(subNode); if (level < MaxLevel) { GetSubDirectoryNodes(subNode, dirSub.FullName, level + 1); } } } private void tvDestFiles_AfterSelect(object sender, TreeViewEventArgs e) { txtTargetDir.Text = tvDestFiles.SelectedNode.FullPath; } private void btnUploadFiles_Click(object sender, EventArgs e) { List fileList = GetFileList(); foreach (FileInfo file in fileList) {
You should be able to catch the System.UnauthorizedAccessException to "skip" a directory you don't have access to.