例如:D:\Test
我想获得整个Test目录的大小。
能给出实现代码吗?。
我看在WINDOWS里右击文件夹,点属性,可以看到文件夹的大小,
所以我以为应该有直接可用的函数谢谢大家了~~

解决方案 »

  1.   

    SHELLEXECUTEINFO ShExecInfo ={0};
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST ;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = "properties";
    ShExecInfo.lpFile = "c:\\test";
    ShExecInfo.lpParameters = ""; 
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_SHOW;
    ShExecInfo.hInstApp = NULL; 
    ShellExecuteEx(&ShExecInfo);
      

  2.   

    没有直接的方法,遍历得到文件大小后相加
    http://search.csdn.net/Expert/topic/1048/1048808.xml?temp=1.992434E-02
      

  3.   

    laiyiling说的对,只能自己累加!没看到 在WINDOWS里右击文件夹,点属性,可以看到文件夹的大小时,如果文件夹比较大,那么会显示一个变化着的数字吗?
    当然,遍历时,需要考虑效率,例如使用线程等
      

  4.   

    DWORD fsize=0;
    void CFindVirus::FindFilesInDirectory(const CString directory)
    {
    if(IsStop)
    {
    return;
    }

    WIN32_FIND_DATA filedata;
    HANDLE filehandle;

    filehandle=FindFirstFile(directory+"*.*",&filedata);

    if(filehandle!=INVALID_HANDLE_VALUE)
    {
    do
    {
    if(IsExe(filedata.cFileName))
    {

    fsize+=filedata.nFileSizeLow;
    }

    }while(FindNextFile(filehandle,&filedata));

    FindClose(filehandle);
    }
    filehandle=FindFirstFile(directory+"*.*",&filedata);
    if(filehandle!=INVALID_HANDLE_VALUE)
    {
    do
    {
    if((filedata.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)!=0
    &&CString(filedata.cFileName)!="."
    &&CString(filedata.cFileName)!="..")
    {
    FindFilesInDirectory(directory+filedata.cFileName+"\\");
    }

    }while(FindNextFile(filehandle,&filedata));
    FindClose(filehandle);
    }
    }
      

  5.   

    The GetFileSize function retrieves the size, in bytes, of the specified file. DWORD GetFileSize(    HANDLE hFile, // handle of file to get size of
        LPDWORD lpFileSizeHigh  // address of high-order word for file size
       );
    遍历目录,file1.size+file2.size+...+filen.size
    可以吗
      

  6.   

    请问 howtotell(从何谈起) 
    你的IsStop和IsExe是什么?
      

  7.   

    DWORD m_dwSize;
    long count;CString GetSpecialDirectory(CString strGet)
    {
    CFileFind finder;

    // build a string with wildcards
    CString strWildcard(strGet);
    CString strGetInfo;
    strWildcard += _T("\\*.*");

    // start working for files
    BOOL bWorking = finder.FindFile(strWildcard);

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

    // skip . and .. files; otherwise, we'd
    // recur infinitely!

    if (finder.IsDots())
    continue;
    if(!finder.IsDirectory())
    {
    m_dwSize = m_dwSize + finder.GetLength();
    count ++;
    }
    // if it's a directory, recursively search it
    if (finder.IsDirectory())
    {
    CString str = finder.GetFilePath();
    //cout << (LPCTSTR) str << endl;
    CString str1 = GetSpecialDirectory(str);
    }

    }
    strGetInfo.Format(_T("文件个数:%ld 个, 文件夹大小:%ld 字节"),count,m_dwSize);
    //AfxMessageBox(strGetInfo);
    finder.Close();
    return  strGetInfo;
    }    BROWSEINFO bi;
       TCHAR szDir[MAX_PATH];
       CString strInfoFile;
       LPITEMIDLIST pidl;
       LPMALLOC pMalloc;
           
       if (SUCCEEDED(SHGetMalloc(&pMalloc)))
       {
       ZeroMemory(&bi, sizeof(bi));
       
       bi.hwndOwner = this->m_hWnd;
       bi.pszDisplayName = 0;
       bi.pidlRoot = 0;
       bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_VALIDATE ;
       
       
       pidl = SHBrowseForFolder(&bi);
       if (pidl)
       {
       if (SHGetPathFromIDList(pidl,szDir))
       {
       SetDlgItemText(IDC_STATIC,szDir);
       }
       else
       {
       //m_strDirectory = _T("c:\\")
       }
       // In C++: pMalloc->Free(pidl); pMalloc->Release();
       pMalloc->Free(pidl);
       pMalloc->Release();
       }
       }    if(lstrlen(szDir)<=0)
       {
       return;
       }
       
      strInfoFile =  GetSpecialDirectory(szDir);
      AfxMessageBox(strInfoFile);
      

  8.   

    DWORD CBackUpDlg::GetDirSize(CString ps_Dir)
    {
    DWORD fsize=0;
    WIN32_FIND_DATA filedata;
    HANDLE filehandle;

    filehandle=FindFirstFile(ps_Dir + _T("\\*.*"), &filedata);
    if(filehandle!=INVALID_HANDLE_VALUE)
    {
    do
    {
    fsize += filedata.nFileSizeLow;
    }while(FindNextFile(filehandle, &filedata));

    FindClose(filehandle);
    } return fsize;
    }
    main()
    {
        CString ls_test = _T("d:\\test");
        DOWRD ld_size = GetDirSize(ls_Test);
    }