获取C ++ Win32中的目录大小 [英] Get Directory Size in C++ Win32

查看:300
本文介绍了获取C ++ Win32中的目录大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取指定的目录大小包含api函数中的所有文件和文件夹或带有递归扫描目录的示例代码?



有什么方法可以获得目录大小没有递归扫描???



谢谢

How can i Get Specified directory size included all files and folders in it with api function or sample code with recursively scan directory ?

Is there any way to get directory size without recursively scan ???

Thanks

推荐答案

我通过Google搜索找到了这些解决方案:



这里可以找到纯粹的Win32 C ++示例:如何查找位于文件夹内的所有文件的大小 [ ^ ]



如果在C ++应用程序中启用.NET Framework托管代码,则可以使用:

扫描目录/文件(计算大小) [ ^ ]。它确实需要递归。



您可以在Native C ++项目中轻松启用Managed C ++。如果您计划使用.NET System.IO.Directory,则可以使用以下设置调用本机C ++项目。



启用/ clr在项目属性中切换 - >一般 - >公共语言运行时支持)。



现在,您的项目将支持.NET Framework类。您可以在编程中直接使用托管C ++代码。



注意:

我不认为那里是没有递归扫描的任何方式。想想如果每次向文件添加内容时,它会如何减慢您的计算机速度,Windows必须更新文件所在的所有目录节点。
I found these solutions via Google search:

A pure Win32 C++ example can be found here: How to find the size of all files, located inside a folder[^]

If you enable .NET Framework managed code in your C++ application, you can use this:
Scan the Directory / Files (Calculating the size)[^]. It does require recursion.

You can enable Managed C++ easily within your Native C++ project. If you are planning to use .NET System.IO.Directory, you can call in your native C++ project using the following setting.

Enable the "/clr" switch in Project Properties -> General -> Common Language Run time Support).

Now, your project will support .NET Framework classes. You can directly use managed C++ code in your programming.

Note:
I don't think there is any way to do it without a recursive scan. Think about how it would slow down your computer if every time you added something to a file, Windows had to update all the directory nodes above where your file was located.


如果您正在寻找这里是一个不使用.NET的纯Win32 API解决方案:

If you a looking for a pure Win32 API solution without using .NET here is what you do:
WIN32_FIND_DATAA data;
    HANDLE sh = NULL;

    sh = FindFirstFileA((_path+"\\*").c_str(), &data);

    if (sh == INVALID_HANDLE_VALUE )
    {
            return;
    }

    do
    {
        // skip current directory and parent directory
        if (std::string(data.cFileName).compare(".") != 0 &&  
            std::string(data.cFileName).compare("..") != 0)
        {

            // if found object is a ("...")
            if ((data.dwFileAttributes & 
                 FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY)
            {
                // we have reached a directory entry, 
               // then search inside of it recursively
                this->CalculateSize(_path + ''\\'' + data.cFileName);
            } 
            else
            {
                // get entry's size and add it to overall size
                this->dirSize += (__int64) (data.nFileSizeHigh *(MAXDWORD) +  
                                            data.nFileSizeLow);
            }
        }

    } while (FindNextFileA(sh, &data)); // do

    FindClose(sh);



希望有所帮助。


Hope that helps.


参见 FindFirstFile [ ^ ]及其相关的 FindNextFile 函数,扫描目录。如果要包含所有子目录,则需要使代码以递归方式执行扫描。
See FindFirstFile[^] and its associated FindNextFile functions, to scan through a directory. You will need to make your code do the scans recursively if you wish to include all subdirectories.


这篇关于获取C ++ Win32中的目录大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆