Search code examples
c#winformsfileinfogetfiles

How do I get and display all the file sizes from the file array


I am new to programming, currently trying to get all the file sizes from the file array and display next to them. I found the solution which is FileInfo, but have no idea how it works and couldn't find any solution online. The file array retrieved and display successfully before I added the FileInfo line.

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

I have a button which can open the folder dialog and user are able to select the directory, and a listbox to display all the file/directory from the selected path. Can I actually get all the files sizes along the path and display next to the files/ directory?


Solution

  • Not with Directory.GetFiles you can't - it returns an array of strings that are filepaths. You'd have to make a new FileInfo from each one and get its length.. It'd be better to call the method of DirectoryInfo that returns you an array of FileInfo to start with:

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

    I'm not really sure what you mean by "Can I actually get all the files sizes along the path and display next to the .. directory"

    Directories don't have a file size; did you mean that you want the sum total of all the files' sizes inside the directory? For all subdirectories in the hierarchy or the top directory only? Perhaps something like this:

    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)");
            }
        }  
    }
    

    For Sum to work you'll have to have imported System.Linq


    Incidentally, I present the following as a commentary on why your code didn't work:

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