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