猿问

关于VC如何实时获取文件夹内文件数量的问题?求指教

是文件夹内的任何文件数量,我需要同时监控3个文件(无需多线程,有更好),数量反馈给编辑框就可以了

梵蒂冈之花
浏览 145回答 2
2回答

慕丝7291255

int CountDirectory(CString path) {     int count = 0;     CFileFind finder;     BOOL working = finder.FindFile(path + "\\*.*");     while (working)    {        working = finder.FindNextFile();         if (finder.IsDots())             continue;         if (!finder.IsDirectory())             count++;     }     return count; }以上为不递归子目录的统计代码,如果文件不是非常多,那么添加这个函数,然后在对话框的OnTimer定时器响应函数中用定时器做:void CDialog1::OnTimer(UINT nIDEvent) {    // TODO: Add your message handler code here and/or call default    if (nIDEvent==1)    {        int i = CountDirectory("目录1");        CString str;        str.Format("%d",i);        GetDlgItem(IDC_EDIT1)->SetWindowText(str);    }    CDialog::OnTimer(nIDEvent);} 注意在适当的时候(如OnInitDialog中)SetTimer(1,5000);就可以了

撒科打诨

文件个数:遍历文件夹,CFindFile找"*.*",设个变量,找到文件,就加个1,这个很容易实现的。你要怎样监控文件?监控指定文件存在与否?
随时随地看视频慕课网APP
我要回答