gpt4 book ai didi

c# - 根据大小计算文件组

转载 作者:太空宇宙 更新时间:2023-11-03 18:16:05 24 4
gpt4 key购买 nike

我怎样才能简化这个?我正在尝试根据文件的大小从目录和子目录中获取 Excel 文件的数量。我至少有 10 个不同的分组。

var queryList2Only = from i in di.GetFiles("*.xls", SearchOption.TopDirectoryOnly)
.Where(f => f.Length <= 5120)
select i.Length;
if (queryList2Only.Any())
{
dest.WriteLine("Excel File <= 5 KB");
dest.WriteLine(queryList2Only.Count());
dest.WriteLine("");
}

var queryList3Only = from i in di.GetFiles("*.xls", SearchOption.TopDirectoryOnly)
.Where(f => f.Length > 5120 && f.Length <= 10240)
select i.Length;
if (queryList3Only.Any())
{
dest.WriteLine("Excel File > 5 KB and <= 10 KB");
dest.WriteLine(queryList3Only.Count());
dest.WriteLine("");

编辑:我需要这个

  <= 5 KB,> 5 KB and <= 10 KB,> 10 KB and <= 20 KB,> 20 KB and <= 100 KB,> 100 KB and <= 1000 KB,> 1000 KB and <=5 MB,> 5 MB and <=10 MB,> 10 MB and <=20 MB,> 20 MB and <=50 MB,> 50 MB and <=100 MB

private void button1_Click(object sender, EventArgs e)
{



DirectoryInfo Folder = new DirectoryInfo(textBox1.Text);
var _logFolderPath4 = Path.Combine(textBox1.Text.Trim(), "log");
if (Folder.Exists)

if (!Directory.Exists(_logFolderPath4))
Directory.CreateDirectory(_logFolderPath4);

DirectoryInfo di = new DirectoryInfo(@"D:\Material\");
bool time = false;
using (var dest = File.AppendText(Path.Combine(_logFolderPath4, "Excel.txt")))
{

if (!time)
{
dest.WriteLine("---------------------" + DateTime.Now + "---------------------");
dest.WriteLine("");
time = true;
}
CountFiles(dest, di, @"*.txt");
}

}

最佳答案

您需要将您的范围放在一个集合中,并枚举它们。这是一个应该让你开始的例子 - sizes 数组包含步骤,当然你应该选择对你的应用程序有意义的步骤:

int[] sizes = Enumerable.Range(0,10).Select(n => (int)Math.Pow(2,n + 8)).ToArray();
int lower = 0;
foreach(var size in sizes)
{
var files = di.GetFiles("*.*").Where(f => f.Length >= lower && f.Length < size);
Console.WriteLine("Between {0} and {1} bytes:", lower,size);
foreach(var file in files)
Console.WriteLine("\t{0}",file);
lower = size;
}

关于c# - 根据大小计算文件组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6151137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com