【问题标题】:How can i get a list of the names of all files with a size less than 70MB如何获取大小小于 70MB 的所有文件的名称列表
【发布时间】:2015-09-10 21:06:02
【问题描述】:
string[] fileEntries = Directory.GetFiles(@"d:\", "*.avi");
        public static string filetoupload;
        int count = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            if (fileEntries.Length > 0)
            {
                filetoupload = fileEntries[count];
                count += 1;
            }
        }

        private void FilesSizes()
        {
            string[] sizes = { "B", "KB", "MB", "GB" };
            double len = new FileInfo(filetoupload).Length;
            int order = 0;
            while (len >= 1024 && order + 1 < sizes.Length)
            {
                order++;
                len = len / 1024;
            }
            string result = String.Format("{0:0.##} {1}", len, sizes[order]);
        }

现在 fileEntries 包含所有的 avi 文件。 但我希望它使用 FilesSizes 方法仅包含不超过 70MB 的 avi 文件。

我尝试过这种方式但出现错误:

private void FilesSizes()
        {
            for (int i = 0; i < fileEntries.Length; i++)
            {
                string[] sizes = { "B", "KB", "MB", "GB" };
                double len = new FileInfo(fileEntries[i]).Length;
                int order = 0;
                while (len >= 1024 && order + 1 < sizes.Length)
                {
                    order++;
                    len = len / 1024;
                }
                string result = String.Format("{0:0.##} {1}", len, sizes[order]);

                if (len > 70)
                    fileEntries[i].Remove(i);
            }
        }

首先我不确定执行 i​​f (len > 70) 是否是检查文件大小是否大于 70MB 的正确方法。

我在这一行遇到的第二个错误:

fileEntries[i].Remove(i);

问题是例如当 i = 14 时: startIndex 必须小于字符串长度

我现在也尝试返回一个列表

private List<string> FilesSizes()
        {
            List<string> filestoremove = null;
            foreach(string item in fileEntries)
            {
                string[] sizes = { "B", "KB", "MB", "GB" };
                double len = new FileInfo(item).Length;
                int order = 0;
                while (len >= 1024 && order + 1 < sizes.Length)
                {
                    order++;
                    len = len / 1024;
                }
                string result = String.Format("{0:0.##} {1}", len, sizes[order]);

                if (len > 70)
                {
                    filestoremove = new List<string>(fileEntries);
                    filestoremove.Remove(item);
                }

            }

            return filestoremove;
        }

但由于某种原因,它只从列表中删除了一个文件,并且有 22 个文件,还有一个超过 100MB,所以我做错了。

【问题讨论】:

  • 你的新代码做了很多它不需要做的事情,你不能把 70MB 转换成字节然后检查FileInfo.Length吗?
  • List&lt;FileInfo&gt; files = new DirectoryInfo(@"d:\").GetFiles("*.avi").Where(f =&gt; f.Length &lt;= 70 * 1024 * 1024).ToList();
  • @Loathing 这应该是一个答案,对其他用户也更有用:)

标签: c# .net winforms


【解决方案1】:

一种方法是修改FilesSizes 方法,使其返回文件大小。然后在按钮单击中,您可以循环遍历fileEntries 数组并找到每个条目的文件大小。如果文件大小合适,那么您可以对它做一些事情:添加到一个新数组(或者更好的是,List&lt;string&gt;)或附加到另一个字符串。

您可以通过使用 Linq 查询文件系统来使其更加优雅。一个有趣的例子在这里:https://msdn.microsoft.com/en-us/library/bb546154.aspx

一旦您尝试了某事,如果您需要更多帮助,请发布您的代码。

【讨论】:

    猜你喜欢
    • 2014-06-27
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多