C#C
C#3y ago
nks1fu

❔ ✅ Creating a console application in C# that utilizes async/await functions.

hi so i have to create a console application and the porpuse is to etrieve information on file sizes and the number of files in a specified directory. Your application should be able to handle multiple directory paths as input.

there were asyinc functions i had to use and want to see anyway i can change it for the better or make it simpler

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace FileSizeCalculator
{
    class Program
    {
        static async Task Main(string[] args)
        {

            Console.WriteLine("Enter directory paths separated by commas:");
            string input = Console.ReadLine();
            List<string> directoryPaths = new List<string>(input.Split(','));

            long totalSize = await GetTotalSizeAsync(directoryPaths);


            Console.WriteLine($"Total number of files: {GetFileCount(directoryPaths)}");
            Console.WriteLine($"Total size of files (bytes): {totalSize}");
        }

        static async Task<int> GetFileCountAsync(string directoryPath)
        {
            string[] files = await Task.Run(() => Directory.GetFiles(directoryPath));
            return files.Length;
        }

        static async Task<long> GetFileSizeAsync(string filePath)
        {

            FileInfo fileInfo = await Task.Run(() => new FileInfo(filePath));
            return fileInfo.Length;
        }

     
`
Was this page helpful?