Win32 API 中获取文件夹下文件列表的函数是哪个呀?知道的朋友告诉下.

2024-11-23 11:23:51
推荐回答(4个)
回答1:

没有文件列表,只能枚举所有文件,再根据文件的属性判断是文件或者文件夹。
下面是MSDN上的完整例子
#include
#include
#include
#include
#pragma comment(lib, "User32.lib")

void DisplayErrorBox(LPTSTR lpszFunction);

int _tmain(int argc, TCHAR *argv[])
{
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
TCHAR szDir[MAX_PATH];
size_t length_of_arg;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError=0;

// If the directory is not specified as a command-line argument,
// print usage.

if(argc != 2)
{
_tprintf(TEXT("\nUsage: %s \n"), argv[0]);
return (-1);
}

// Check that the input path plus 3 is not longer than MAX_PATH.
// Three characters are for the "\*" plus NULL appended below.

StringCchLength(argv[1], MAX_PATH, &length_of_arg);

if (length_of_arg > (MAX_PATH - 3))
{
_tprintf(TEXT("\nDirectory path is too long.\n"));
return (-1);
}

_tprintf(TEXT("\nTarget directory is %s\n\n"), argv[1]);

// Prepare string for use with FindFile functions. First, copy the
// string to a buffer, then append '\*' to the directory name.

StringCchCopy(szDir, MAX_PATH, argv[1]);
StringCchCat(szDir, MAX_PATH, TEXT("\\*"));

// Find the first file in the directory.

hFind = FindFirstFile(szDir, &ffd);

if (INVALID_HANDLE_VALUE == hFind)
{
DisplayErrorBox(TEXT("FindFirstFile"));
return dwError;
}

// List all the files in the directory with some info about them.

do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
_tprintf(TEXT(" %s

\n"), ffd.cFileName);
}
else
{
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
_tprintf(TEXT(" %s %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
}
}
while (FindNextFile(hFind, &ffd) != 0);

dwError = GetLastError();
if (dwError != ERROR_NO_MORE_FILES)
{
DisplayErrorBox(TEXT("FindFirstFile"));
}

FindClose(hFind);
return dwError;
}

void DisplayErrorBox(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code

LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );

// Display the error message and clean up

lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}

回答2:

SHBrowseForFolder可以提供文件列表,再用API函数SHGetPathFromIDList可以获得具体的路径,如果用户按的是“取消”按钮,则返回值为NULL

定义:
Option Explicit

Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Public Const BIF_RETURNONLYFSDIRS = &H1

Public pidl As Long

Public Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long

Public Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

SHGetPathFromIDList 中pidl参数使用SHBrowseForFolder的返回值,pszPath返回函数调用结束后用户选择的路径

回答3:

vb吗
可以这样,一个按钮,两个列表框
Public Function GetDirList(strPath As String)
Dim DirBox
Set DirBox = Controls.Add("VB.DirListBox", "DirListBox")
DirBox.path = strPath
For i = 0 To DirBox.ListCount - 1
GetDirList = GetDirList & DirBox.List(i) & vbCrLf
Next
End Function

Public Function GetFilesList(strPath As String)
Dim FileBox
Set FileBox = Controls.Add("VB.FileListBox", "FileListBox")
FileBox.path = "c:\"
For i = 0 To FileBox.ListCount - 1
GetFilesList = GetFilesList & FileBox.List(i) & vbCrLf
Next
End Function

Private Sub Command1_Click()
Dim a As String
a = GetDirList("c:\")
For i = 0 To UBound(Split(a, vbCrLf))
List1.AddItem Split(a, vbCrLf)(i)
Next

a = GetFilesList("c:\")
For i = 0 To UBound(Split(a, vbCrLf))
List2.AddItem Split(a, vbCrLf)(i)
Next

End Sub

回答4:

类定义在C:\Program Files\Microsoft Visual Studio\VC98\MFC\Include目录下的.h头文件中

api函数是在.dll文件中的,不是VC的一部分,在系统文件夹下:C:\WINDOWS\system32

C函数在C:\Program Files\Microsoft Visual Studio\VC98\Lib目录下的.lib文件中