0

I'm using Asp.net treeview to show my directory including files. I want to show the file path once the user click on the treeview node. I'm using FullName property to get the path. The problem that I have is, treeview shows the full path only for the directory not for the file!

Here is my code

private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
{
    TreeNode directoryNode = new TreeNode(directoryInfo.Name);

    foreach (DirectoryInfo directory in directoryInfo.GetDirectories())
    {
        if (!directory.Attributes.ToString().Contains("Hidden"))
        {
            directoryNode.ChildNodes.Add(CreateDirectoryNode(directory));
            directoryNode.Value = directoryInfo.FullName; // Here I'm passing the directory path
        }
    }

    foreach (FileInfo file in directoryInfo.GetFiles())
    {
        if (File.GetAttributes(file.FullName).ToString().IndexOf("Hidden") == -1)
        {
            directoryNode.ChildNodes.Add(new TreeNode(file.Name));
            directoryNode.Value = file.FullName; // Here I'm passing the file path
        }
    }

    return directoryNode;
}

Update For some reason full path is not showing treeNode value for the file but directory!

HardCode
  • 2,025
  • 4
  • 33
  • 55

2 Answers2

1

You set value to the wrong node.

Change

  directoryNode.ChildNodes.Add(new TreeNode(file.Name));  
  directoryNode.Value = file.FullName; // Here I'm passing the file path  

To

  TreeNode fileNode = new TreeNode(file.Name, file.FullName);
  directoryNode.ChildNodes.Add(fileNode);

This will set Value of the file node to it`s Full path

Nogard
  • 1,779
  • 2
  • 18
  • 21
0

The Value property is not displayed

Change

directoryNode.ChildNodes.Add(new TreeNode(file.Name));

To

directoryNode.ChildNodes.Add(new TreeNode(file.FullName));
mlorbetske
  • 5,529
  • 2
  • 28
  • 40
  • Yes, it does work but I don't want to show the full file path on the tree node. Instead I want to pass it as a value. – HardCode Oct 22 '12 at 17:20
  • I'm confused, what is it you're asking then? It sounded like you wanted the full path to be displayed on the file node – mlorbetske Oct 22 '12 at 17:23
  • No, If you check my code, I'm trying to pass the FullName to treeNode value. Sorry, I need to be bit more clear, I'm trying to get the treeNode value and I couldn't get the full path. – HardCode Oct 22 '12 at 17:26