On Tue, 27 Jan 2004 12:31:00 -0500, Jennica Humphrey <jj*****@STOPSP AM.rit.edu> wrote:
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 GetFileSystemEn tries().
using ArgumentExcepti on = System.Argument Exception;
using ArgumentNullExc eption = System.Argument NullException;
using Console = System.Console;
using Directory = System.IO.Direc tory;
using DirectoryNotFou ndException = System.IO.Direc toryNotFoundExc eption;
using Exception = System.Exceptio n;
using SecurityExcepti on = System.Security .SecurityExcept ion;
class Startup
{
public static void Main()
{
try
{
// Obtain the file system entries in the directory path "c:\".
string[] directoryEntrie s = Directory.GetFi leSystemEntries ( "c:\\" );
foreach( string s in directoryEntrie s )
{
Console.WriteLi ne( s );
}
}
catch( ArgumentNullExc eption )
{
Console.Error.W riteLine( "Path is a null reference." );
}
catch( ArgumentExcepti on )
{
Console.Error.W riteLine(
"Path is an empty string, "
+ "contains only white spaces, "
+ "or contains invalid characters."
);
}
catch( SecurityExcepti on )
{
Console.Error.W riteLine(
"The caller does not have the required permission."
);
}
catch( DirectoryNotFou ndException )
{
Console.Error.W riteLine(
"The path encapsulated in the Directory object does not exist."
);
}
catch( Exception x )
{
Console.Error.W riteLine( "Unexpected exception: " + x );
}
}
} // class Startup