Listing files within a directory in Windows

J

Jennica Humphrey

Hi, just as the topic says, how do I get the names of files within a
directory? The examples I've seen appear to be *nix-specific. I'm
using .NET but would like to avoid MFC if at all possible.
 
R

Rolf Magnus

Jennica said:
Hi, just as the topic says, how do I get the names of files within a
directory? The examples I've seen appear to be *nix-specific. I'm
using .NET but would like to avoid MFC if at all possible.

Thre is no standard C++ way to do what you want. You have to use some OS
specific functions. You should ask this question in a windows
programming newsgroup.
 
A

Alf P. Steinbach

Hi, just as the topic says, how do I get the names of files within a
directory? The examples I've seen appear to be *nix-specific. I'm
using .NET but would like to avoid MFC if at all possible.

MFC isn't .NET.

That aside, here's how to do it in .NET using C#:

======================================================================
// C#.
// Adapted from MSDN documentation of GetFileSystemEntries().

using ArgumentException = System.ArgumentException;
using ArgumentNullException = System.ArgumentNullException;
using Console = System.Console;
using Directory = System.IO.Directory;
using DirectoryNotFoundException = System.IO.DirectoryNotFoundException;
using Exception = System.Exception;
using SecurityException = System.Security.SecurityException;

class Startup
{
public static void Main()
{
try
{
// Obtain the file system entries in the directory path "c:\".
string[] directoryEntries = Directory.GetFileSystemEntries( "c:\\" );

foreach( string s in directoryEntries )
{
Console.WriteLine( s );
}
}
catch( ArgumentNullException )
{
Console.Error.WriteLine( "Path is a null reference." );
}
catch( ArgumentException )
{
Console.Error.WriteLine(
"Path is an empty string, "
+ "contains only white spaces, "
+ "or contains invalid characters."
);
}
catch( SecurityException )
{
Console.Error.WriteLine(
"The caller does not have the required permission."
);
}
catch( DirectoryNotFoundException )
{
Console.Error.WriteLine(
"The path encapsulated in the Directory object does not exist."
);
}
catch( Exception x )
{
Console.Error.WriteLine( "Unexpected exception: " + x );
}
}
} // class Startup
 
A

Alf P. Steinbach

MFC isn't .NET.

That aside, here's how to do it in .NET using C#:

Oops, I apologize for thinking this was [comp.os.ms-windows.programmer.win32]!

Jennica, please repost your question in that group.

This group only discusses the C++ programming language, so the question is
off-topic here.
 
J

Jennica Humphrey

Oops, I apologize--I meant, I wanted to do this in C++ without having to
use MFC... I only mentioned .NET because I wanted to emphasize that I
didn't have access to unistd.h or whatever.

but it looks like there's no way to do it without using MFC or a
3rd-party product, so will go ahead and use the MFC methods supplied by
MS... ugh... thanks.
 
E

Evan Carew

Jennica,

The "Boost Filesystem Library" is a facility which attempts to be a drop
in replacement for "<fstream>" in the STL. Along with the obvious
functionality, the Boost library provides all the little dodads you
would expect for basic file & directory manipulation.

Evan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top