April 24, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

Calculate average file size for a folder with C#

If you need to have an estimation of the average file size of a folder containing couple of files ? Easy with C#

 

class Program
{
 static void Main(string[] args)
 {
  string[] dirfiles = Directory.GetFiles("c:\\software\\");            
  var avg = dirfiles.Select(file =>  
                  new FileInfo(file).Length).Average();
  avg = Math.Round(avg/1000000, 1);
  Console.WriteLine("The Average file size is {0} MB",
   avg);
  Console.ReadLine();
 }
}

If you need to have an estimation of the average file size of a folder containing couple of files ? Easy with C#

 

class Program
{
 static void Main(string[] args)
 {
  string[] dirfiles = Directory.GetFiles("c:\\software\\");            
  var avg = dirfiles.Select(file =>  
                  new FileInfo(file).Length).Average();
  avg = Math.Round(avg/1000000, 1);
  Console.WriteLine("The Average file size is {0} MB",
   avg);
  Console.ReadLine();
 }
}