Reading directories in MFC

I recently have been working on a c++ client server program, the beta version of the server is complete, and I'm working on the client, but the client i would like to create in MFC for visual effects. One problem i found is that I need to read in the content of a Directory. I found a way in C++ to do this unsing opendir(), readdir() and a few other functions. I wrote a Directory class to make it easier, but now in MFC i can't use any of the opendir() or readdir() functions because they use c++ files that won't work with MFC. the problem occurred in including the files sys/types.h and dirent.h. Is there any way to get this to work, or to read a directory using MFC functions. I would like if possible to do this all in code and not dialog boxes, so lets assume that the folder name is in a char *. any way i can do this?
Who is Participating?
jackrabbit22Connect With a Mentor Commented:
Try using FindFirstFile, FindNextFile functions.

The following will place all files in a folder into a CStringArray.

The folderpath passed to the function is assumed to have a trailing slash. i.e. C:\Windows\Desktop\ vs. C:\Windows\Desktop

void ListFolderContents(LPCTSTR lpszFolderPath, CStringArray& fileListing)
     WIN32_FIND_DATA findData;

        // Add the wildcard extension
     TCHAR curFolder[MAX_PATH];
     _tcscpy(curFolder, lpszFolderPath);
     _tcscat(curFolder, _T("*.*"));

     HANDLE handle = FindFirstFile(curFolder, &findData);

     if (handle == INVALID_HANDLE_VALUE)

     // Process all files within current directory
          TRACE(_T("Filename: %s\n"), findData.cFileName);

          // Add file to our array

          // If you want to test if a file is a folder
//         if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

          // Be sure to check for . and .. folders
     } while (FindNextFile(handle, &findData));
Forget something...

Add this line after the do/while loop:

necroticworldAuthor Commented:
Thanx that helped alot.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.