473,657 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Listing files within a directory in Windows

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.
Jul 22 '05 #1
7 2371
Jennica Humphrey 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.


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.

Jul 22 '05 #2
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
Jul 22 '05 #3
On Tue, 27 Jan 2004 18:10:05 GMT, al***@start.no (Alf P. Steinbach) wrote:
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#:


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

Jennica, please repost your question in that group.

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

Jul 22 '05 #4

Jennica Humphrey 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.


Try http://www.boost.org/libs/filesystem/doc/index.htm

regards,
alexander.
Jul 22 '05 #5

"Alf P. Steinbach" wrote:
[...]
here's how to do it in .NET using C#: ...


And here's how to do it in C++ [uh, silly catch(std::exce ption const &)
aside for a moment]:

http://www.boost.org/libs/filesystem.../simple_ls.cpp

regards,
alexander.
Jul 22 '05 #6
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.

Alf P. Steinbach wrote:
On Tue, 27 Jan 2004 18:10:05 GMT, al***@start.no (Alf P. Steinbach) wrote:

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.

Jul 22 '05 #7
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

Jennica Humphrey 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.


Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2050
by: brett | last post by:
On my website I have a directory filled with possible files that a user can select to download. Rather than going to the directory itself to download, I'd like the user to be able to select a single file from a select box embedded on an html page. I'm looking for a way that I can use javascript to do this for me. I know that perl could get these filenames for me easily, but is javascript able to put these into a select box once I have Perl...
19
3304
by: SU News Server | last post by:
I've struggled with this for quite a while and I'm am just not sure what is going on. I have the following code import os def buildList( directory='/Users/mkonrad' ) dirs = listing = os.listdir(directory)
3
2040
by: David Jacques | last post by:
I am trying to get a list of all files of a certain extension type on disk to do some processing in a loop. The code needs to be portable to UNIX, so I need to use plain c functionality. Does anyone know of a way to do this ? Any URLs, code snippets, etc ?
11
2111
by: F. Michael Miller | last post by:
I'd like to copy the listing of a directory (& sub directories) to a text file in vb.net. I'm looking for teh equivilant of the DOS command dir /s > TargetFile.txt. Thanks!
8
11060
by: gil | last post by:
Is it possible to prevent a browser from listing the entire contents of a folder? The site, is hosted on my ISP with the following layout- site/ "user name from ISP" pagefile (dir) index.html site/pagefile/
2
1700
by: ngr | last post by:
I used to use the DirListox control in VB6 but this is no longer supported in VB.NET. What I want to do: I want to take a directory listing and show any subdirectories in a list. When you click on a directory in the list another list appears with the directory listing of files and directories within it. Can anyone please advise how to take a list of directories within a directory and then independantly of that another routine to take...
8
6943
by: dougawells | last post by:
Hi - I'm hoping for help with the auto-generation of a hyperlinked listing of all files in a directory. The server I use does not auto-generate this. So, when someone comes to this directory and thus opens index.htm, I want that file to show a listing of all files in the directory with hyperlinks to them. Thanks for the help, Doug
7
5484
by: epikto | last post by:
I have a mapped share that I am trying to get a listing of all the files that it contains. I use the following code to access the contents String files = Directory.GetFiles(path); I can then enumerate through that array to get my listing. However I am trying to set my application up as a service. And now (after calling methods in a thread using the OnStart() method) it says the directory does not exist (I verified this with a...
2
1344
by: Mike P | last post by:
Is it possible to write some code to print to the screen a listing of all files within a directory? I want to show the path of each file on screen and enable the user to download any of the files. *** Sent via Developersdex http://www.developersdex.com ***
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8622
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.