10
Answers

How to call the method in the Page load?

Here this is my program
 
 
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SecondWeekContinues
{
public partial class TreeViewDemo : Form
{
public TreeViewDemo()
{
InitializeComponent();
ListDirectory(,"D:\\");    //See i am getting error here
}
private void ListDirectory(TreeView treeView, string path)
{
treeView1.Nodes.Clear();
var stack = new Stack<TreeNode>();
var rootDirectory = new DirectoryInfo(path);
var node = new TreeNode(rootDirectory.Name) { Tag = rootDirectory };
stack.Push(node);
while (stack.Count > 0)
{
var currentNode = stack.Pop();
var directoryInfo = (DirectoryInfo)currentNode.Tag;
foreach (var directory in directoryInfo.GetDirectories())
{
var childDirectoryNode = new TreeNode(directory.Name) { Tag = directory };
currentNode.Nodes.Add(childDirectoryNode);
stack.Push(childDirectoryNode);
}
foreach (var file in directoryInfo.GetFiles())
currentNode.Nodes.Add(new TreeNode(file.Name));
}
treeView1.Nodes.Add(node);
}
}
}
 

Answers (10)

0
Photo of Gnanavel Sekar
NA 6.8k 452.3k 8y
if stuck anywhere let me know, i will help you out.
 
same example here refer below link
http://stackoverflow.com/questions/6239544/populate-treeview-with-file-system-directory-structure 
Accepted
0
Photo of Tapan Patel
NA 8.1k 101k 8y
As you are calling method after Initialzing components, you can pass this.treeview1 as first argument.
 
  1. ListDirectory(this.treeView1,"D:\\");    
 
0
Photo of Thennarasu N
NA 4.3k 74.6k 8y
 please Look at this i hope it Will help you 
 
  http://stackoverflow.com/questions/18024240/call-a-method-on-page-load
0
Photo of Bryian Tan
NA 9.4k 887.4k 8y
Based on your code, both the parameters for ListDirectory method are require.
  1.  private void ListDirectory(TreeView treeView, string path)  
  2. {  
  3. ...  

Look like the TreeView control name is treeView1, you can modify the ListDirectory method to
  1.  private void ListDirectory(string path)  
  2. {  
  3. ...  

Then on the TreeViewDemo()
  1.    
  2. public TreeViewDemo()  
  3. {  
  4.    ListDirectory ("D:\\");  

Regarding the TreeView, look like the code is working fine, it displaying both folders and files. Did you click on the + sign to expand the folder?
 
0
Photo of chetan Allipur
NA 413 33.7k 8y
I am not undertsanding. if you have time please send me the code(I should be Execute). I want both directory and files.
0
Photo of chetan Allipur
NA 413 33.7k 8y
Actully i want to display the files also, but it is only displaying folders in Treeview
 
0
Photo of chetan Allipur
NA 413 33.7k 8y
Getting Error: 
Error 1 Optional parameters must appear after all required parameters
0
Photo of chetan Allipur
NA 413 33.7k 8y
I dont know what sould i pass the value to the first argument
0
Photo of Gnanavel Sekar
NA 6.8k 452.3k 8y
May i know what error you are getting?
-1
Photo of Marimuthu Karuppaiah
NA 230 17.3k 8y
Try out this also.
 
http://stackoverflow.com/questions/35159549/how-to-display-all-files-under-folders-in-treeview