Question

I'm trying to bind a TreeView control to the public folders the machine ASUS-PC on my LAN.

ASUS-PC Form Picture

However, I can't figure out how to parse the "ASUS-PC" directory because I am unable to create a DirectoryInfo object from the path "\\\\ASUS-PC".

DirectoryInfo dir = new DirectoryInfo("\\\\ASUS-PC");

The line above throws an ArgumentException with the message The UNC path should be of the form \\server\share.

The code below only creates a dumb node for the root, so I can't display all of the public folders on that PC.

How do I pull the directory listing for the path "\\ASUS-PC"?

public void BindDirectoryToTreeView(string directoryPathToBind) {
  if (!String.IsNullOrEmpty(directoryPathToBind) && Directory.Exists(directoryPathToBind)) {
    treeView1.Nodes.Clear();
    TreeNode rootNode = null;
    char[] ps = treeView1.PathSeparator.ToCharArray();
    string[] split = directoryPathToBind.Split(ps);
    var folders = split.Where(str => !String.IsNullOrEmpty(str));
    for (int i = 0; i < folders.Count(); i++) {
      var folder = folders.ElementAt(i);
      int index = directoryPathToBind.IndexOf(folder);
      int length = index + folder.Length;
      string path = directoryPathToBind.Substring(0, length);
      if (rootNode != null) {
        DirectoryInfo dir = new DirectoryInfo(path);
        TreeNode node = new TreeNode(dir.Name, 0, 0);
        RecurseFolders(dir, node);
      } else {
        //DirectoryInfo dir = new DirectoryInfo(path); <= Throws the Error
        rootNode = new TreeNode(folder, 0, 0);
        //RecurseFolders(dir, rootNode);
      }
    }
    treeView1.Nodes.Add(rootNode);
    if (0 < rootNode.Nodes.Count) {
      rootNode.Expand();
    }
  }
}

I know this can be done. So, what do I need to do this?

Windows Does it

Was it helpful?

Solution

What you are looking for is a way to enumerate network shares on a given host. It is not possible to do this with DirectoryInfo.

OTHER TIPS

\\ASUS-PC is not a directory, that's why DirectoryInfo is complaining. You should find a method for enumerating shares

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top