This code shows how to get folders and subfolders using c#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO;
namespace WindowsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); FillDirectoryTree(tvmTree); }
private void Form1_Load(object sender, EventArgs e) {
} private void FillDirectoryTree(TreeView tvm) { tvm.Nodes.Clear(); string[] strDriver = Environment.GetLogicalDrives(); foreach (string rootDirectoryName in strDriver) { if (rootDirectoryName != @"D:\") continue; try { DirectoryInfo dir = new DirectoryInfo(rootDirectoryName); dir.GetDirectories(); TreeNode ndRoot = new TreeNode(rootDirectoryName); tvm.Nodes.Add(ndRoot); GetSubDirectoryNodes(ndRoot,ndRoot.Text);
} catch (Exception ce) { MessageBox.Show(ce.ToString()); } } } private void GetSubDirectoryNodes(TreeNode parentNode, string fullName) { DirectoryInfo dir = new DirectoryInfo(fullName); DirectoryInfo[] dirSubs = dir.GetDirectories(); foreach(DirectoryInfo dirSub in dirSubs) { if((dirSub.Attributes & FileAttributes.Hidden)!=0) { continue; } TreeNode subNode = new TreeNode(dirSub.Name); parentNode.Nodes.Add(subNode); this.GetSubDirectoryNodes(subNode,dirSub.FullName); } }
private void tvmTree_AfterSelect(object sender, TreeViewEventArgs e) { lvwList.Items.Clear(); String path = e.Node.FullPath; DirectoryInfo dir = new DirectoryInfo(path); path = path.Remove(2, 1); txtPath.Text = path; FileInfo[] files = dir.GetFiles(); for (int i = 0; i < files.Length; i++) { \\adding items to object of listview lvwList.Items.Add(files[i].Name); } }
private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|