biggest *.bak file

  • Thread starter Thread starter kal
  • Start date Start date
K

kal

my requirement is to find the biggest *.bak in a given
folder. I am new to C# and I am having trouble accessing
the file system. Any code snippet or help is appreciated.
 
Not the cleanest implementation -- but it should get you started...

FileInfo largestFile = null;

DirectoryInfo directoryToSearch = new DirectoryInfo(@"c:\temp");
FileInfo[] filesInDirectory = directoryToSearch.GetFiles("*.txt");
foreach(FileInfo currentFile in filesInDirectory)
{
if(largestFile == null)
{
largestFile = currentFile;
}

if(largestFile.Length < currentFile.Length)
{
largestFile = currentFile;
}
}

if(largestFile == null)
{
Console.WriteLine("No files found");
}
else
{
Console.WriteLine("{0} is the largest file ", largestFile.Name);
}

Console.WriteLine("Press any key to continue");
Console.ReadLine();
 
1. Create a DirecoryInfo object that represents your directory.
2. Call the GetFiles(string searchPattern), this will return an array of
FileInfo objects.
3. Check the Length property of each FileInfo object.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top