nks1fu
nks1fu
CC#
Created by nks1fu on 4/8/2023 in #help
❔ ✅ 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;
}


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


`
55 replies
CC#
Created by nks1fu on 2/2/2023 in #help
❔ ✅ beginner class and properties
55 replies
CC#
Created by nks1fu on 12/24/2022 in #help
✅ how can i x amount of times ask the user to input a number and save it in a list
14 replies