C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

get directory and sub directory in c#


Posted Date: 07 Jan 2009    Resource Type: Code Snippets    Category: C# Syntax
Author: taresh sudaMember Level: Silver    
Rating: 1 out of 5Points: 10



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();
}
}
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Treeview and list view  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Parse the Enum value
Previous Resource: Cookies
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use