c# - 如何从文件数组中获取并显示所有文件大小
问题描述
我是编程新手,目前正试图从文件数组中获取所有文件大小并显示在它们旁边。我找到了 FileInfo 的解决方案,但不知道它是如何工作的,也无法在线找到任何解决方案。在我添加 FileInfo 行之前,文件数组已成功检索并显示。
private void Button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog FBD = new FolderBrowserDialog();
if (FBD.ShowDialog()==DialogResult.OK)
{
listBox1.Items.Clear();
string[] files = Directory.GetFiles(FBD.SelectedPath);
string[] dirs = Directory.GetDirectories(FBD.SelectedPath);
foreach (string file in files)
{
long length = new FileInfo(FBD.SelectedPath).Length; //FileNotFoundException
listBox1.Items.Add(Path.GetFileName(file + length));
}
foreach (string dir in dirs)
{
listBox1.Items.Add(Path.GetFileName(dir));
}
}
}
我有一个可以打开文件夹对话框的按钮,用户可以选择目录,还有一个列表框来显示所选路径中的所有文件/目录。我实际上可以获取路径上的所有文件大小并显示在 files/ 目录旁边吗?
解决方案
不是Directory.GetFiles
你不能 - 它返回一个作为文件路径的字符串数组。你必须FileInfo
从每一个中创建一个新的并获取它的长度。最好调用DirectoryInfo
返回一个数组的方法FileInfo
来开始:
private void Button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog FBD = new FolderBrowserDialog();
if (FBD.ShowDialog()==DialogResult.OK)
{
listBox1.Items.Clear();
FileInfo[] files = new DirectoryInfo(FBD.SelectedPath).GetFiles();
string[] dirs = Directory.GetDirectories(FBD.SelectedPath);
foreach (FileInfo file in files)
{
listBox1.Items.Add(file.Name + "(" + file.Length + " bytes)");
}
foreach (string dir in dirs)
{
listBox1.Items.Add(Path.GetFileName(dir));
}
}
}
我不太确定您所说的“我是否可以实际获取路径中的所有文件大小并显示在 .. 目录旁边”
目录没有文件大小;你的意思是你想要目录中所有文件大小的总和吗?仅针对层次结构中的所有子目录或顶级目录?也许是这样的:
private void Button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog FBD = new FolderBrowserDialog();
if (FBD.ShowDialog()==DialogResult.OK)
{
listBox1.Items.Clear();
FileInfo[] files = new DirectoryInfo(FBD.SelectedPath).GetFiles();
DirectoryInfo[] dirs = new DirectoryInfo(FBD.SelectedPath).GetDirectories();
foreach (FileInfo file in files)
{
listBox1.Items.Add(file.Name + "(" + file.Length + " bytes)");
}
foreach (DirectoryInfo dir in dirs)
{
listBox1.Items.Add(dir.Name + "(" + dir.GetFiles().Sum(f => f.Length) + " bytes)");
}
}
}
要使 Sum 工作,您必须导入 System.Linq
顺便说一句,我将以下内容作为您的代码不起作用的评论:
private void Button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog FBD = new FolderBrowserDialog();
if (FBD.ShowDialog()==DialogResult.OK)
{
listBox1.Items.Clear();
string[] files = Directory.GetFiles(FBD.SelectedPath);
string[] dirs = Directory.GetDirectories(FBD.SelectedPath);
foreach (string file in files) //ok, so file is the filepath
{
//it doesn't work because you put "FBD.SelectedPath" in instead of "file"
// FBD.SelectedPath is a directory, not a file, hence the FileNotFoundException
//But the real problem is probably a cut n paste error here
long length = new FileInfo(FBD.SelectedPath).Length;
//it would work out but it's a weird way to do it, adding the length on before you strip the filename out
//Path doesnt do anything complex, it just drops all the text before the
//last occurrence of /, but doing Path.GetFilename(file) + length would be better
listBox1.Items.Add(Path.GetFileName(file + length));
}
foreach (string dir in dirs)
{
//need to be careful here: "C:\temp\" is a path of a directory but calling GetFilename on it would return "", not temp
listBox1.Items.Add(Path.GetFileName(dir));
}
}
}
推荐阅读
- azure - 如何使用 Azure Data Bricks 使用 REST API 端点?
- reactjs - 为什么axios不向asp.core api提交数据
- python - 使用 boto3 在 python 中使用另一个现有表的结构创建 DynamoDB 表时出错
- javascript - javascript:单击编辑时更新文本框中的所有元素
- javascript - Javascript 结合来自 3 个文件的 HTML 来创建表格
- matlab - 为什么solve()会抛出这个结果 Empty sym: 0-by-1 并且无法得到正确的结果
- c++ - 带堆栈的 DFS 在有向、非循环、未加权图中查找某个节点的最长路径长度
- vue.js - 天蓝色地图不会在正确的位置渲染标记
- java - 我的 Java 代码中发生了什么使答案是否定的?
- python - GUI 中的“交互式”图形