C语言 使用 C 和 Windows 列出目录内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2314542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Listing directory contents using C and Windows
提问by Flyer1
I'm looking to list and store the contents of a directory in a struct using C on Windows.
我希望在 Windows 上使用 C 将目录的内容列出并存储在结构中。
I'm not necessarily looking for anyone to write out the code I'm looking for, rather point me in the right direction when it comes to which library I should be looking at.
我不一定要寻找任何人来写出我正在寻找的代码,而是在我应该查看哪个库时为我指明正确的方向。
I've been Googling for a few hours now and all I'm finding is C#, C++ solutions so any help would be greatly appreciated.
我已经在谷歌上搜索了几个小时,我发现的只是 C#、C++ 解决方案,所以任何帮助都将不胜感激。
回答by NTDLS
Just like everyone else said (with FindFirstFile, FindNextFile and FindClose)... but with recursion!
就像其他人说的一样(使用 FindFirstFile、FindNextFile 和 FindClose)……但是使用递归!
bool ListDirectoryContents(const char *sDir)
{
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
char sPath[2048];
//Specify a file mask. *.* = We want everything!
sprintf(sPath, "%s\*.*", sDir);
if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE)
{
printf("Path not found: [%s]\n", sDir);
return false;
}
do
{
//Find first file will always return "."
// and ".." as the first two directories.
if(strcmp(fdFile.cFileName, ".") != 0
&& strcmp(fdFile.cFileName, "..") != 0)
{
//Build up our file path using the passed in
// [sDir] and the file/foldername we just found:
sprintf(sPath, "%s\%s", sDir, fdFile.cFileName);
//Is the entity a File or Folder?
if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
{
printf("Directory: %s\n", sPath);
ListDirectoryContents(sPath); //Recursion, I love it!
}
else{
printf("File: %s\n", sPath);
}
}
}
while(FindNextFile(hFind, &fdFile)); //Find the next file.
FindClose(hFind); //Always, Always, clean things up!
return true;
}
ListDirectoryContents("C:\Windows\");
And now its UNICODE counterpart:
现在它的 UNICODE 对应:
bool ListDirectoryContents(const wchar_t *sDir)
{
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
wchar_t sPath[2048];
//Specify a file mask. *.* = We want everything!
wsprintf(sPath, L"%s\*.*", sDir);
if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE)
{
wprintf(L"Path not found: [%s]\n", sDir);
return false;
}
do
{
//Find first file will always return "."
// and ".." as the first two directories.
if(wcscmp(fdFile.cFileName, L".") != 0
&& wcscmp(fdFile.cFileName, L"..") != 0)
{
//Build up our file path using the passed in
// [sDir] and the file/foldername we just found:
wsprintf(sPath, L"%s\%s", sDir, fdFile.cFileName);
//Is the entity a File or Folder?
if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
{
wprintf(L"Directory: %s\n", sPath);
ListDirectoryContents(sPath); //Recursion, I love it!
}
else{
wprintf(L"File: %s\n", sPath);
}
}
}
while(FindNextFile(hFind, &fdFile)); //Find the next file.
FindClose(hFind); //Always, Always, clean things up!
return true;
}
ListDirectoryContents(L"C:\Windows\");
回答by lollinus
Probably You are looking for these functions: FindFirstFile, FindNextFile, and FindClose.
可能您正在寻找这些函数:FindFirstFile、FindNextFile和FindClose。
回答by lollinus
To list file contents you can search a directory with these APIs: FindFirstFileEx, FindNextFileand CloseFind. You'll need to #include <windows.h>, that'll get you access to the Windows API. They're C functions and so compatible with C++. If you want "specifically C++", try searching for listing directories using MFC.
要列出文件内容,您可以使用以下 API 搜索目录:FindFirstFileEx、FindNextFile和CloseFind。您需#include <windows.h>要这样做,才能访问 Windows API。它们是 C 函数,因此与 C++ 兼容。如果您想要“特别是 C++”,请尝试使用 MFC 搜索列表目录。

