简体   繁体   English

获取长度大小的FileInfo错误

[英]FileInfo Error for getting length size

    private long yy=0;

    private void CopyAll(DirectoryInfo source,DirectoryInfo target )
    {

        if (Directory.Exists(target.FullName) == false)
        {
            try
            {
                Directory.CreateDirectory(target.FullName);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }

        }
        foreach (FileInfo fi in source.GetFiles())
        {
                yy += fi.Length;
                backgroundWorker1.ReportProgress((int)yy / 1024);

        }
        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
        {

                DirectoryInfo nextTargetSubDir =
                    target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);               
        }

The result i get when i do this is a negative number and the wrong value, has this been answered before and does anyone know what am doing wrong? 我执行此操作时得到的结果是负数和错误的值,是否已经回答过此问题,有人知道做错了什么吗?

As asked by other people. 根据其他人的要求。 what is "yy" ? 什么是“ yy”? is it an integer? 是整数吗? a long? 长吗? a byte? 一个字节? a single? 单身? a double? 一双? a string? 一个字符串? Nobody knows. 没人知道。 yy is surrounded in mystery. yy被神秘包围。 I will presume yy is a "long" as for a total file size this is probably the safest way (although still prone to issues). 我认为yy是一个“长”文件,因为这可能是最安全的方式(尽管仍然容易出现问题)。 I will also overlook your variable name mis-match to help with your question. 我也将忽略您的变量名不匹配,以帮助您解决问题。

Light reading material before we head off: https://en.wikipedia.org/wiki/Integer_overflow 我们出发前的轻便阅读材料: https : //en.wikipedia.org/wiki/Integer_overflow

yy i am presuming is your total file size for the source directories files. yy我想是您的源目录文件的总文件大小。 as yy reaches its max value as specified by [datatype].MaxValue in a signed value as it goes over it will set off with what's left from [datatype].MinValue. 当yy达到[datatype] .MaxValue指定的最大值时,有符号值越过它,它将抵消[datatype] .MinValue的剩余值。 break on each iteration, where yy gets added to and add a watch to "yy". 在每次迭代时中断,在其中添加yy并将手表添加到“ yy”。 You will see its value slowly (or quickly, depending on the size of the files in the directory) reach [datatype].MaxValue. 您将看到其值缓慢地(或快速地,取决于目录中文件的大小)达到[数据类型] .MaxValue。 when this is reached and continually added to it will return to MinValue. 当达到并持续添加时,将返回MinValue。

For some interactive learning try out this piece of code: 对于一些交互式学习,请尝试以下代码:

        long val = long.MaxValue;
        val += 1;

inspect val, You will see it's -9223372036854775808. 检查val,您会看到它是-9223372036854775808。 (See previous light learning material) (请参阅以前的灯光学习材料)

Your question has to be "what am I achieving here". 您的问题必须是“我在这里能取得什么成就”。 Do you HAVE to know the total sizes of all files or can you achieve it differently. 您是否必须了解所有文件的总大小,还是可以以不同的方式实现? A common way to achieve extra-large size values is to keep a reference to each unit in its own data store. 实现超大尺寸值的常见方法是在其自己的数据存储区中保留对每个单元的引用。 by unit i mean unit, tens, hundreds, thousands, etc etc etc and add your single.Length to this. 按单位,我的意思是单位,数十,数百,数千等,等等,然后添加您的single.Length。 You will have to write this yourself. 您将必须自己编写。

You can use a float for extra large numbers but it depends entirely on how accurate you need your reading to be. 您可以将浮点数用于更大的数字,但这完全取决于您需要的读数精度。 (floats lose precision as the number gets bigger) (随着数字的增加,浮点数会失去精度)

Edit: also another point to consider after looking over your code again. 编辑:再次查看代码后要考虑的另一点。 you are casting yy to an int in your report progress. 您在报表进度中将yy转换为int。 if yy is long and cast to an int when yy is bigger than int.MaxValue, you will see the same issue. 如果yy长并且当yy大于int.MaxValue时将其强制转换为int,则将看到相同的问题。 why are you casting yy to an int? 为什么将yy转换为int? why not just divide by 1024 on yy directly and then cast to int (still presuming yy is a long) 为什么不直接在yy上除以1024然后将其强制转换为int(仍然假设yy很长)

(int)(yy / 1024)

Edit 2: Following your elaborated code you are recursing through folders you pretty much absolutely have hit MaxValue on your yy value. 编辑2:按照详细的代码,您将遍历几乎绝对已经在yy值上达到MaxValue的文件夹。

Edit 3: Why do you care about total size of all files when you are writing what appears to be a "Copy" method to copy an entire directory structure across. 编辑3:为什么在编写看起来像“复制”方法来复制整个目录结构的文件时,为什么还要关心所有文件的总大小? (Notice you make directories in the destination directory but do not copy any files over so it appears to be a directory structure copy and not data) If you want to do some kind of "is there enough space in the destination to do this copy" you should calculate space before doing anything in the destination location. (请注意,您在目标目录中建立目录,但不要复制任何文件,因此它似乎是目录结构的副本,而不是数据。)如果要执行某种“目标中是否有足够的空间来执行此复制”您应该在目标位置执行任何操作之前先计算空间。

Good luck! 祝好运!

Directory.GetFiles() will give you a list of Strings. Directory.GetFiles()将为您提供字符串列表。 you cannot convert it directly as FileInfo that's the problem here. 您不能将其直接转换为FileInfo ,这就是这里的问题。 FileInfo.Length will return size of file in bytes , so range of integer is not enough to hold the value if the file size is large. FileInfo.Length将返回文件大小(以bytes ,因此,如果文件大小较大,则整数范围不足以容纳该值。 so check the datatype of yy 所以检查yy的数据类型

So i suggest you to do something like the following: 因此,我建议您执行以下操作:

  foreach (string F in Directory.GetFiles("D:\YourDirectory"))
     {
         FileInfo tempFileInfo = new FileInfo(F);
         yy += tempFileInfo.Length;
         backgroundWorker1.ReportProgress((int)yy / 1024);
     } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM