Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Directory listing

Directory listing

Scheduled Pinned Locked Moved C#
csharptoolshelpworkspace
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wistiti5
    wrote on last edited by
    #1

    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) {

    J 1 Reply Last reply
    0
    • W wistiti5

      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) {

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      You should be able to catch the System.UnauthorizedAccessException to "skip" a directory you don't have access to.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups