using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string substringDirectory;
public Form1()
{
InitializeComponent();
directoryTreeView.Nodes.Clear();
String path = "E:\\Mohit";
directoryTreeView.Nodes.Add(path);
PopulateTreeView(path, directoryTreeView.Nodes[0]);
}
public void PopulateTreeView(string directoryValue, TreeNode parentNode)
{
string[] directoryArray =
Directory.GetDirectories(directoryValue);
try
{
if (directoryArray.Length != 0)
{
foreach (string directory in directoryArray)
{
substringDirectory = directory.Substring(
directory.LastIndexOf('\\') + 1,
directory.Length - directory.LastIndexOf('\\') - 1);
TreeNode myNode = new TreeNode(substringDirectory);
parentNode.Nodes.Add(myNode);
PopulateTreeView(directory, myNode);
}
}
}
catch (UnauthorizedAccessException)
{
parentNode.Nodes.Add("Access denied");
} // end catch
}
}
}
No comments:
Post a Comment