【问题标题】:How do i check for all the files sizes in a directory while in a loop?如何在循环中检查目录中的所有文件大小?
【发布时间】:2013-08-09 20:50:57
【问题描述】:
s = Environment.GetEnvironmentVariable("UserProfile") + "\\Pictures";
            string[] photosfiles = Directory.GetFiles(t, "*.*", SearchOption.AllDirectories);
            for (int i = 0; i < s.Length; i++)
            {

                File.Copy(photosfiles[i], tempphotos + "\\" + Path.GetFileName(photosfiles[i]), true);

            }

这会将文件从一个目录复制到另一个目录。 我想一直在 FOR 循环内检查目标目录的大小。 例如,首先它复制一个文件以检查文件大小是否小于 50mb 继续。

复制第二个文件后循环中的下一个迭代检查目标目录大小中的两个文件,如果两个文件大小小于 50mb,则继续。 依此类推,直到达到 50mb,然后停止循环。

【问题讨论】:

标签: c# winforms


【解决方案1】:

您可以参考以下代码:

string[] sizes = { "B", "KB", "MB", "GB" };
    double len = new FileInfo(filename).Length;
    int order = 0;
    while (len >= 1024 && order + 1 < sizes.Length) {
        order++;
        len = len/1024;
    }

    // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
    // show a single decimal place, and no space.
    string result = String.Format("{0:0.##} {1}", len, sizes[order]);

static String BytesToString(long byteCount)
{
    string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
    if (byteCount == 0)
        return "0" + suf[0];
    long bytes = Math.Abs(byteCount);
    int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
    double num = Math.Round(bytes / Math.Pow(1024, place), 1);
    return (Math.Sign(byteCount) * num).ToString() + suf[place];
}

两个答案都来自链接How do I get a human-readable file size in bytes abbreviation using .NET?

【讨论】:

    【解决方案2】:

    您可以在开始复制文件之前计算目录的大小,然后在复制文件时添加每个文件的大小,或者您可以在每个文件复制后重新计算目录的大小。我认为后一种选择可能会更准确,但效率也会低得多,具体取决于您要复制的文件的大小(如果它们非常小,您最终会计算它们很多次)。

    要获取目录的大小,请使用:

    public static long DirSize(DirectoryInfo d) 
    {    
        long Size = 0;    
        // Add file sizes.
        FileInfo[] fis = d.GetFiles();
        foreach (FileInfo fi in fis) 
        {      
            Size += fi.Length;    
        }
        // Add subdirectory sizes.
        DirectoryInfo[] dis = d.GetDirectories();
        foreach (DirectoryInfo di in dis) 
        {
            Size += DirSize(di);   
        }
        return(Size);  
    }
    

    此处用作示例的函数: http://msdn.microsoft.com/en-us/library/system.io.directory(v=vs.100).aspx

    所以你的代码看起来像:

    for (int i = 0; i < photosfiles.Length; i++)
    {
        FileInfo fi(photosfiles[i]);
    
        DirectoryInfo d = new DirectoryInfo(tempphotos);
        long dirSize = DirSize(d);
    
        //if copying the file would take the directory over 50MB then don't do it
        if ((dirSize + fi.length) <= 52428800)
            fi.CopyTo(tempphotos + "\\" + fi.Name)
        else
            break;
    }
    

    【讨论】:

    • 这是有效的。现在我如何检查大小是否达到 50mb ?这是代码: for (int i = 0; i
    • 您只需将字节除以 1024 两次(或 1048576)。或者您可以与 52428800(50MB 字节)进行比较。后一种选择会更准确。
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 2021-08-29
    • 1970-01-01
    • 2019-08-24
    • 2012-01-20
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多