首页 > 解决方案 > 如何从文件数组中获取并显示所有文件大小

问题描述

我是编程新手,目前正试图从文件数组中获取所有文件大小并显示在它们旁边。我找到了 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/ 目录旁边吗?

标签: c#winformsfileinfogetfiles

解决方案


不是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));
        }
    }  
}

推荐阅读