DAS_2023_1/almukhammetov_bulat_lab_2/Part1/Program.cs
2024-01-02 13:36:35 +04:00

55 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var directoryPath = Environment.GetEnvironmentVariable("APP_DIR");
if (directoryPath == null)
{
Console.WriteLine("Не найден переменная среды адреса");
return;
}
var inputFilesPath = Path.Combine(directoryPath, "data");
var outputFilePath = Path.Combine(directoryPath, "result");
Console.WriteLine($"Входная папка: {inputFilesPath}");
Console.WriteLine($"Выходная папка: {outputFilePath}");
try
{
if (Directory.Exists(inputFilesPath))
{
var files = Directory.GetFiles(inputFilesPath);
if (files.Length > 0)
{
var fileWithMostLines = files
.Select(filePath => new { FilePath = filePath, LineCount = File.ReadLines(filePath).Count() })
.OrderByDescending(x => x.LineCount)
.First();
Console.WriteLine($"Файл с наибольшим количеством строк: {fileWithMostLines.FilePath}");
Console.WriteLine($"Количество строк в файле: {fileWithMostLines.LineCount}");
string outputFileName = "inputFileFor2Program.txt";
string outputFilePathWithFileName = Path.Combine(outputFilePath, outputFileName);
if (File.Exists(outputFilePathWithFileName))
{
Console.WriteLine($"Файл {outputFilePathWithFileName} уже существует удалим его");
File.Delete(outputFilePathWithFileName);
}
File.Copy(fileWithMostLines.FilePath, outputFilePathWithFileName);
Console.WriteLine($"Файл перемещен в {outputFilePathWithFileName}");
}
else
{
Console.WriteLine("В директории нет файлов.");
}
}
else
{
Console.WriteLine("Указанной директории не существует.");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}