DAS_2024_1/yakovleva_yulia_lab_2/FileGeneration/Program.cs

42 lines
1.1 KiB
C#
Raw Normal View History

2024-09-20 18:36:39 +04:00
using System;
using System.IO;
class Program
{
static void Main()
{
string directoryPath = Environment.GetEnvironmentVariable("DATA_PATH") ?? ".\\data";
Random random = new Random();
try
{
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
for (int i = 1; i <= 100; i++)
{
int lineCount = random.Next(1, 101);
string filePath = Path.Combine(directoryPath, $"file_{i}.txt");
using (StreamWriter writer = new StreamWriter(filePath))
{
for (int j = 0; j < lineCount; j++)
{
writer.WriteLine($"{j + 1}");
}
}
Console.WriteLine($"Файл {filePath} успешно создан с {lineCount} строками.");
}
}
catch (Exception ex)
{
Console.WriteLine("Произошла ошибка: " + ex.Message);
}
}
}