Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 08-12-2008
Member
 
Join Date: Oct 2008
Posts: 54
List all Files and Directories in a Directory

We can list all the files in a directory. If we come across a directory our program should again explore it and find files within that directory.

Code:
using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.IO ;

 

public class Class1

{

static int filecount, dircount ;

static void Main( string[] args )

{

string path = "D:\test" ;

DirectoryInfo d = new DirectoryInfo ( path ) ;

Check ( d ) ;

Console.WriteLine ( "Total Files :" + filecount ) ;

Console.WriteLine ( "Total Directories :" + dircount ) ;

}

public static void Check ( DirectoryInfo d )

{

FileSystemInfo[] f = d.GetFileSystemInfos() ;

foreach ( FileSystemInfo i in f )

{

if (i.GetType().ToString()== "System.IO.DirectoryInfo" )

{

dircount++ ;

DirectoryInfo d1=new DirectoryInfo ( i.FullName ) ;

Console.WriteLine ( i.FullName ) ;

Check ( d1 ) ;

}

else

{

Console.WriteLine ( i.Name ) ;

filecount++ ;

}

}

}

}
Just like the FileInfo class, we have a DirectoryInfo class. To the constructor of this DirectoryInfo class we passed the path our user gave us. Recursion is the key to this program. We call a check function which checks whether the name passed is of a directory or a file. If it?s a file we print the name but if it?s a directory we pass the path to the Check( ) function again . Here we?ve used an array of FileSystemInfo. By using the GetFileSystemInfos( ) we get all entries in the supplied path. We ran a for loop on the entries. We also kept a count on the Directories and Files to get the total files and directories.

Quote:
Output:

D:\test

D:\test\lucpp

CHAP3.DOC

CHAP2.DOC

CHAP1.DOC

CHAP4.DOC

CHAP6.DOC

CHAP7.DOC

CHAP8.DOC

CHAP10.DOC

CHAP11.DOC

CHAP12.DOC

CHAP13.DOC

CHAP5.DOC

BACKCVR.DOC

CHAP9.DOC

CPPBOOK.DOT

INTRO.DOC

TITLE.DOC

text1.txt

text2.txt

text3.txt

Total Files :20

Total Directories :1
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "List all Files and Directories in a Directory"
Thread Thread Starter Forum Replies Last Post
scripts to list all users, home directories, and their home directories sizes ngwodo Technology & Internet 3 16-07-2011 03:03 AM
Change Read Only Directory Attributes, all drives and directories in windows 7 $OLIVER$ Operating Systems 3 28-01-2011 01:19 PM
Distinguishing between files and directories Flacos Software Development 3 05-12-2009 04:13 PM
Website submission, art link directories list required? skyjill86 Operating Systems 2 25-11-2009 03:44 AM
Batch file to list directories, export to CSV chickenfriedsteak Windows Server Help 1 16-02-2009 08:35 PM


All times are GMT +5.5. The time now is 08:22 AM.