л/р 2
This commit is contained in:
40
tasks/yudenicheva-ds/lab_2/worker-1/Program.cs
Normal file
40
tasks/yudenicheva-ds/lab_2/worker-1/Program.cs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user