Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 25th, 2003, 05:50 AM
Authorized User
 
Join Date: Jun 2003
Location: Edinburgh, , United Kingdom.
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default file/directory iteration

Hi

I want to find a way to iterate through files in a directory.

E.g. I have a directory and I want to loop round all files and return each file name.

Thx

Nickie
 
Old July 25th, 2003, 06:10 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Nickie,

Take a look at this thread.

The code is in VB.NET but it should be easy to change it for c#. The example loops through a starting folder, recursing each sub folder it contains and also loops through the Files collection for each folder.

HtH

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old July 26th, 2003, 01:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Hi Nickie

Make use of Directory class of System.IO
This class has got functions like GetFiles, GetDirectories
that just suit your requirement.

Ankur Verma
.Net and C++ Specialist
Wiley Tech Support
 
Old July 28th, 2003, 03:09 AM
Authorized User
 
Join Date: Jun 2003
Location: Edinburgh, , United Kingdom.
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Thx Ankur, and I did a bit of digging around after the last post and found this code. Just what I need.

Cheers

Nickie
 
Old August 1st, 2003, 05:29 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to merediths
Default


This bit is snipped from a quick-and-dirty app I coded to verify the transfer of a 1.6 Tb file system from one server to another, but it should give you an idea. the Name property of the FileSystemInfo object is what you are looking for...

    foreach(FileSystemInfo fInfo in dir.GetFileSystemInfos()){
            dirCount++;
            fcount = 0;
            if(fInfo is DirectoryInfo){
            // Console.WriteLine("Recursing {0}...", fInfo.Name);
                DirectoryInfo subDir = new DirectoryInfo(Path.GetFullPath(dir.Name + "\\" + fInfo.Name));
                foreach(FileSystemInfo subfInfo in subDir.GetFileSystemInfos()){
                    fcount++;
                    epscount=0;

                Console.Write("{0}\n", fcount);
            }
        }

Regards,
Meredith Shaebanyan

 
Old August 1st, 2003, 05:30 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to merediths
Default

Sorry .. forgot the constructor; args[0] should be the path to the directory you want to enumerate.

    DirectoryInfo dir = new DirectoryInfo(Path.GetFullPath(args[0]));
        Console.WriteLine("{0},", Path.GetFullPath(dir.ToString()));





Similar Threads
Thread Thread Starter Forum Replies Last Post
iteration bostek Excel VBA 5 August 30th, 2006 01:23 AM
File Directory Access ra7207 VB.NET 1 June 20th, 2006 01:45 PM
Generated file not in directory ... :( bebe XML 2 August 11th, 2005 08:48 AM
Iteration through a dropdown. tryxxter PHP How-To 1 February 9th, 2005 11:05 PM
Q. How do I upload a file to a directory? richard.york PHP FAQs 0 April 8th, 2004 05:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.