Avatar of techbro
techbro
Flag for United States of America asked on

Finding the largest file in the directory

I am studying for MCTS exam, and coding the largest file in the directory in the practice test.
The problem I am facing is in Line 43, which contains the maximum file size. This code is giving me all name and file sizes, but I only need to largest one. If I place the line 43 after the loop, then variables will be out of scope/unreachable. What changes do I need to make to get only one largest file for each folder?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace DrivesFolders
{
    //Populate with the local computer's drives, folders, and files
    class Program
    {
        
        static void Main(string[] args)
        {
         //   foreach (DriveInfo di in DriveInfo.GetDrives())
            {
           //     Console.WriteLine(" {0}   ({1})", di.Name, di.DriveType);
                
           //     DirectoryInfo dir = new DirectoryInfo(@di.Name);
                DirectoryInfo dir = new DirectoryInfo(@"C:\");
                
                
                foreach (DirectoryInfo dirinfo in dir.GetDirectories())
                {
                    Console.WriteLine("\n\nFolder");
                    Console.WriteLine("--------------");
                    Console.WriteLine(dirinfo.Name);
                    Console.WriteLine("\nFiles");
                    Console.WriteLine("-----");
                    foreach (FileInfo fi in dirinfo.GetFiles())
                    {
                        long max=0;
                   //     long min =0;
                        string maxLength ="";
                        if (fi.Length > max)
                        {
                            maxLength = fi.Name;
                            max = fi.Length;
                        }
                        else
                            break;
                        Console.WriteLine("{0}   ({1})", maxLength, max);
                    }
                             
                }               
                
            }
            Console.ReadKey();
        }
    }
}

Open in new window

C#

Avatar of undefined
Last Comment
techbro

8/22/2022 - Mon
SOLUTION
russellC

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Mike Tomlinson

Right...declare them outside the loop; line #23 would work great.  =)
SOLUTION
Bob Learned

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
techbro

ASKER
kaufmed,
You are right. May be the I need to remove the "break" statement after "else".
I actually thought "foreach" statement is checking each and every file.
Here is what I thought in sequence:
1. File1 is greater than "max" (size = 0), then variables are initialized "maxLength = File1" and "max =100" variables.
2. File2 (size 50) is less than "max" (size = 100), Nothing Happens
3. File3 (size 200) is greater than "max" (size 100), then variables are initialized "maxLength = File3" and "max = 200".

Output: File3  200 (maximum file size)
Let me know if I got this right or missing something. Thank you for noticing.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
kaufmed

I actually thought "foreach" statement is checking each and every file.
Yes, you can't know which is the largest file without checking every file, unless you had some function that returned the files in an order sorted by size. I don't believe the Framework has such a built-in function, though.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
techbro

ASKER
Thank you for your response,
I have received multiple solutions for the problem I have, so I am distributing points to all participants.