This commit is contained in:
2023-12-19 02:38:18 +03:00
parent d321c59542
commit af9f959de1
16 changed files with 735 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
using System.Text;
using System.IO;
string sourceDirectoryPath = "/var/data";
string destinationFilePath = "/var/result/data.txt";
try
{
DirectoryInfo sourceDirectory = new DirectoryInfo(sourceDirectoryPath);
FileInfo[] files = sourceDirectory.GetFiles();
if (files.Length == 0)
{
Console.WriteLine("Каталог {0} не содержит файлов", sourceDirectoryPath);
return;
}
Random random = new Random();
FileInfo randomFile = files[random.Next(files.Length)];
File.Copy(randomFile.FullName, destinationFilePath, true);
Console.WriteLine("Файл {0} скопирован в {1}", randomFile.FullName, destinationFilePath);
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("Каталог {0} не найден", sourceDirectoryPath);
}
catch (IOException e)
{
Console.WriteLine("Произошла ошибка при копировании файла: {0}", e.Message);
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("Отсутствует доступ к файлу {0}", e.Message);
}
catch (ArgumentException)
{
Console.WriteLine("Путь {0} содержит недопустимые символы", destinationFilePath);
}