懂windows api,及CreateFile函数的人才可以进来,解决的给100分

cpsoft 2003-11-20 09:28:18
加精
本人近日编程需要解决遍历文件目录的功能,
查阅MSDN2003,里面有以下这个函数,不知道干什么的和怎么用.
BOOL ReadDirectoryChangesW(
HANDLE hDirectory,
LPVOID lpBuffer,
DWORD nBufferLength,
BOOL bWatchSubtree,
DWORD dwNotifyFilter,
LPDWORD lpBytesReturned,
LPOVERLAPPED lpOverlapped,
LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
在他的说明里面有下面的代码:
hDir = CreateFile(
DirName, // pointer to the file name
FILE_LIST_DIRECTORY, // access (read/write) mode
FILE_SHARE_READ|FILE_SHARE_DELETE, // share mode
NULL, // security descriptor
OPEN_EXISTING, // how to create
FILE_FLAG_BACKUP_SEMANTICS, // file attributes
NULL // file with attributes to copy
);
注意FILE_LIST_DIRECTORY标志,本人是第一次见到,估计是打开目录的.
那么怎么用才能实现我要的功能.解决的给100分!

...全文
137 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
holy198048 2003-12-01
  • 打赏
  • 举报
回复
关注中
changlele 2003-12-01
  • 打赏
  • 举报
回复
可以了为什么不结贴啊
carbon107 2003-11-21
  • 打赏
  • 举报
回复
1.目录监视ReadDirectoryChangesW
用ReadDirectoryChangesW就可以知道发生改变的文件名

http://www.vchelp.net/itbookreview/view_paper.asp?paper_id=487
这里有个例子


2.
HANDLE CreateFile( LPCTSTR lpFileName, // 文件名指针
 DWORD dwDesiredAccess, // 访问模式
 DWORD dwShareMode, // 共享模式
 LPSECURITY_ATTRIBUTES lpSecurityAttributes, //安全属性
 DWORD dwCreationDisposition, // 创建方式
 DWORD dwFlagsAndAttributes, // 文件属性
 HANDLE hTemplateFile // 指向待复制属性的文件指针
);
cpsoft 2003-11-21
  • 打赏
  • 举报
回复
小弟英文差,能不能详细说说ReadDirectoryChangesW的用法.
还有小弟我只用WIN API,有没有不用MFC的方法.
记得在DOS下,当年是用"list >> t.txt"然后对文件分析实现.
现在不会那么老土了.
微软,一定有解决的方法吧.比方MFC怎么实现??
事实上,我看过很多软件的插件只要拷贝到文件夹里就可以使用了,我也是想实现这个功能!
nonocast 2003-11-21
  • 打赏
  • 举报
回复
Advanced Windows上有DI
nonocast 2003-11-21
  • 打赏
  • 举报
回复
GZ
Ah 2003-11-21
  • 打赏
  • 举报
回复
关注
abrams 2003-11-21
  • 打赏
  • 举报
回复
AreFileApisANSI Determines whether the file I/O functions are using the ANSI or OEM character set code page.
CheckNameLegalDOS8Dot3 Determines whether the specified name can be used to create a file on the FAT file system.
CloseHandle Closes an open object handle.
CopyFile Copies an existing file to a new file.
CopyFileEx Copies an existing file to a new file.
CopyProgressRoutine An application-defined callback function used with CopyFileEx and MoveFileWithProgress.
CreateFile Creates or opens a file system object.
CreateHardLink Establishes an NTFS hard link between an existing file and a new file.
DeleteFile Deletes an existing file.
FindClose Closes the specified search handle.
FindFirstFile Searches a directory for a file whose name matches the specified file name.
FindFirstFileEx Searches a directory for a file whose name and attributes match those specified.
FindFirstStreamW Enumerates the first stream in the specified file.
FindNextFile Continues a file search.
FindNextStreamW Continues a stream search.
GetBinaryType Determines whether a file is executable.
GetCompressedFileSize Retrieves the actual number of bytes of disk storage used to store a specified file.
GetFileAttributes Retrieves attributes for a specified file or directory.
GetFileAttributesEx Retrieves attributes for a specified file or directory.
GetFileInformationByHandle Retrieves file information for a specified file.
GetFileSize Retrieves the size of a specified file.
GetFileSizeEx Retrieves the size of a specified file.
GetFileTime Retrieves the date and time that a file was created, last accessed, and last modified.
GetFileType Retrieves the file type of the specified file.
GetFullPathName Retrieves the full path and file name of a specified file.
GetLongPathName Converts the specified path to its long form.
GetShortPathName Retrieves the short path form of a specified input path.
GetTempFileName Creates a name for a temporary file.
GetTempPath Retrieves the path of the directory designated for temporary files.
MoveFile Moves an existing file or a directory.
MoveFileEx Moves an existing file or a directory.
MoveFileWithProgress Moves a file or directory.
ReOpenFile Reopens the specified file system object with different access rights, sharing mode, and flags.
ReplaceFile Replaces one file with another file.
SearchPath Searches for the specified file.
SetFileApisToANSI Causes the file I/O functions to use the ANSI character set code page.
SetFileApisToOEM Causes the file I/O functions to use the OEM character set code page.
SetFileAttributes Sets a file's attributes.
SetFileSecurity Sets the security of a file or directory object.
SetFileShortName Sets the short name for the specified file.
SetFileTime Sets the date and time that a file was created, last accessed, or last modified.
SetFileValidData
fbmsf 2003-11-21
  • 打赏
  • 举报
回复
ReadDirectoryChangesW是用来监视文件夹里面的内容的变化的。
但它只用于Nt/2000, 9x是不支持的。
另外
FILE_LIST_DIRECTORY
是本来是一个File Security and Access Rights(文件安全和访问权限)里面的 。

FILE_LIST_DIRECTORY 说明如下:
FILE_LIST_DIRECTORY ----------For a directory, the right to list the contents of the directory.
cpsoft 2003-11-21
  • 打赏
  • 举报
回复
谢谢,好象可以了!
changlele 2003-11-21
  • 打赏
  • 举报
回复
这是一个遍历目录并且删除目录的一个函数并且删除目录下的所有子目录和文件
bool deletedirectoryex(const string &p)
{
if(p.empty() || p.length()<4)return false; // 参数长度必须大于3,即不能为磁盘根目录或空白
int len=p.length();
const char *path=p.c_str();
string dir=path;

if(path[len-1]!='\\') dir+='\\';
string files=dir+"*.*";
WIN32_FIND_DATA wfd;
HANDLE hfind=FindFirstFile(files.c_str(),&wfd);
bool ret=true;
string tmp;
if(hfind!=INVALID_HANDLE_VALUE)
{
bool bfind=true;
while(bfind)
{
if(wfd.cFileName[0]!='.') // . ..
{
tmp=dir+wfd.cFileName;
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{ // 删除所有子目录
ret=ret&&deletedirectoryex(tmp.c_str());
}else
{ // 删除所有文件
SetFileAttributes(tmp.c_str(),FILE_ATTRIBUTE_NORMAL);
ret=ret&&DeleteFile(tmp.c_str());
}
}
bfind=(FindNextFile(hfind,&wfd)>0?true:false);
}
FindClose(hfind);
}
if(ret) return true;//RemoveDirectory(path);
return false;
}

回去试试
Oversense 2003-11-20
  • 打赏
  • 举报
回复
void findfile(const string& name,vector<string>& files){

CFileFind finder;

CString strWildcard(name.c_str());

BOOL bWorking = finder.FindFile(strWildcard);

while (bWorking)
{
bWorking = finder.FindNextFile();

// skip . and .. files;
if (finder.IsDots())
continue;

if (finder.IsDirectory())
continue;

files.push_back((LPCSTR)finder.GetFileName());
}
finder.Close();
}
dzqsuper 2003-11-20
  • 打赏
  • 举报
回复
友情UP
huanyun 2003-11-20
  • 打赏
  • 举报
回复
3)添加遍历文件夹的函数BrowseFile(int CallNum, CString strFile)
//CalNum-用来记录第几层目录,strFile-路径
huanyun 2003-11-20
  • 打赏
  • 举报
回复
void CFileTreeDlg::BrowseFile(int CallNum, CString strFile)
{
CallNum++;
CFileFind ff;
CString szDir = strFile;

if(szDir.Right(1) != "\\")
szDir += "\\";

szDir += "*.*";

BOOL res = ff.FindFile(szDir);
while(res)
{
res = ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots())//目录是文件夹
{
//如果是一个子目录,用递归继续往深一层找

CString strPath = ff.GetFilePath(); //得到路径,做为递归调用的开始
CString strTitle = ff.GetFileTitle();//得到目录名,做为树控的结点
int i =0;
switch(CallNum)
{
case 1:
strHTFir = m_FileTree.InsertItem(strTitle,0,0,NULL);
break;
case 2:
strHTSec = m_FileTree.InsertItem(strTitle,0,0,strHTFir);
break;
case 3:
strHtThi = m_FileTree.InsertItem(strTitle,0,0,strHTSec);
break;
case 4:
strHtFor = m_FileTree.InsertItem(strTitle,0,0,strHtThi);
break;
default:
strHtFif = m_FileTree.InsertItem(strTitle,0,0,strHtFor);
break;
}
BrowseFile(CallNum,strPath);//递归调用
}
else if(!ff.IsDirectory() && !ff.IsDots())//到达最低层的文件
{
//显示当前访问的文件
CString strPath;
CString strTitle;
strPath = ff.GetFilePath();
strTitle = ff.GetFileTitle();
switch(CallNum)
{
case 1:
strRoot = m_FileTree.InsertItem(strTitle,0,0,NULL);
break;
case 2:
strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHTFir);
break;
case 3:
strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHTSec);
break;
case 4:
strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHtThi);
break;
case 5:
strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHtFor);
break;
default:
strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHtFif);
break;
}
}
}
ff.Close();//关闭
}
huanyun 2003-11-20
  • 打赏
  • 举报
回复
ReadDirectoryChanges
监控文件夹的改变
FILE_LIST_DIRECTORY 没用过也不了解

通常我是用递归遍历目录的

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧