Thursday, August 21, 2008

Display Directory

(Display files tree for Given folder or drive )

The given code will help you to generate directory tree (all directories and files under given folder) for a given folder you will need to use treeview control and folderbrowsedialog control in your form along with a button control to open browse folder and a textbox to contain path of the required directory to be displayed as tree in treeview

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folderBrowserDialog1.SelectedPath;
string[] dirinfo = Directory.GetFileSystemEntries(textBox1.Text);
TreeNode mainnode = new TreeNode(textBox1.Text );
treeView1.Nodes.Add(mainnode);
TreeNode tn = new TreeNode();
foreach (string str in dirinfo)
{

if (Directory.Exists(str))
{
try
{
tn = mainnode.Nodes.Add(str);
// MessageBox.Show("node added" + str, "tn");
ProcessDirectory(tn, str);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
else
{
if (File.Exists(str))
ProcessFile(mainnode, str);
}
}

}


//add directory as node and process it further recursively public static void ProcessDirectory(TreeNode parentnode, string targetDirectory)
{
try
{
TreeNode pn = new TreeNode();
// MessageBox.Show("node added" + targetDirectory, "pn");
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
ProcessFile(parentnode, fileName);
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
{
pn = parentnode.Nodes.Add(targetDirectory);
ProcessDirectory(pn, subdirectory);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}

// for processing found files in each folder.

public static void ProcessFile(TreeNode pn, string path)
{
pn.Nodes.Add(path);
//Console.WriteLine("Processed file '{0}'.", path);

}

Same Thing


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace TreeDir
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//try { Directory.CreateDirectory(@"e:\qwe1"); } catch (Exception ex) {label1.Text = ex.Message; }



if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folderBrowserDialog1.SelectedPath;
string[] dirinfo = Directory.GetFileSystemEntries(textBox1.Text);
TreeNode mainnode = new TreeNode(textBox1.Text);
treeView1.Nodes.Add(mainnode);
TreeNode tn = new TreeNode();
foreach (string str in dirinfo)
{

if (Directory.Exists(str))
{
try
{
tn = mainnode.Nodes.Add(str);
// MessageBox.Show("node added" + str, "tn");
ProcessDirectory(tn, str);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
else
{
if (File.Exists(str))
ProcessFile(mainnode, str);
}

}
}
}

public static void ProcessFile(TreeNode pn, string path)
{
pn.Nodes.Add(path);
//Console.WriteLine("Processed file '{0}'.", path);

}

public static void ProcessDirectory(TreeNode parentnode, string targetDirectory)
{
try
{

TreeNode pn = new TreeNode();

// MessageBox.Show("node added" + targetDirectory, "pn");

// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
ProcessFile(parentnode, fileName);

// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
{
pn = parentnode.Nodes.Add(targetDirectory);
ProcessDirectory(pn, subdirectory);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}}

-----------------

No comments: