1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| private void Form1_Load(object sender, EventArgs e)
{
try
{
//On efface le contenu du TreeView
tvExp.Nodes.Clear();
string[] drives = Environment.GetLogicalDrives();
//Pour chaque lecteur...
foreach (string drv in drives)
{
TreeNode node = new TreeNode();
node.Text = drv;
tvExp.Nodes.Add(node);
FillDirectory(drv, node, 0);
}
}
catch (Exception ex)
{
ex.ToString();
}
}
private void FillDirectory(string drv, TreeNode parent, int level)
{
try
{
level++;
if (level > 4)
return;
DirectoryInfo dir = new DirectoryInfo(drv);
if (!dir.Exists)
throw new DirectoryNotFoundException
("Le répertoire n'existe pas :" + drv);
foreach (DirectoryInfo di in dir.GetDirectories())
{
TreeNode child = new TreeNode();
child.Text = di.Name;
parent.Nodes.Add(child);
FillDirectory(child.FullPath, child, level);
}
}
catch (Exception ex)
{
ex.ToString();
}
} |
Partager