You are here:

C#/list filed on computer

Advertisement


Question
Hi

can you please give me an example of how to list all directories/files on a computer and send them to a file called list.

Thanks

Answer
hi

name space to use

using System.IO;


Get files from directory



string[] filePaths = Directory.GetFiles(@"c:\MyDir\");

// returns:
// "c:\MyDir\my-car.BMP"
// "c:\MyDir\my-house.jpg"



Get files from directory (with specified extension)


string[] filePaths = Directory.GetFiles(@"c:\dir\", "*.bmp");

// returns:
// "c:\MyDir\my-car.BMP"


Get files from directory (including all subdirectories)


string[] filePaths = Directory.GetFiles(@"c:\dir\", "*.bmp",SearchOption.AllDirectories);
// returns:
// "c:\MyDir\my-car.BMP"
// "c:\MyDir\Friends\james.BMP"



Happy programming!

-Srini

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Srini Nagarajan

Expertise

can answer any kind of questions in ASP.NET, C#, VB.NET, ASP, SharePoint 2007, Coldfusion, Powerbuilder 7.00 / 8.00, JAVA servlets, MS SQL 2000 / MSSQL7, Sybase

Experience

Contact me if you need any custom development on ASP.NET, ASP, Coldfusion, Powerbuilder

©2012 About.com, a part of The New York Times Company. All rights reserved.