distributed-computing/tasks/nikiforova-ts/Lab_2/worker-1/Program.cs

34 lines
987 B
C#
Raw Normal View History


using System;
using System.IO;
using System.Text;
2023-12-02 10:45:25 +04:00
string[] files = Directory.GetFiles("/var/data");
string minFilePath = "";
long minFileNameLength = long.MaxValue;
foreach (var filePath in files)
{
FileInfo fileInfo = new FileInfo(filePath);
string fileName = Path.GetFileName(filePath);
if (fileName.Length < minFileNameLength)
{
minFileNameLength = fileName.Length;
minFilePath = filePath;
}
}
using (FileStream sourceStream = File.OpenRead(minFilePath))
{
byte[] buffer = new byte[sourceStream.Length];
await sourceStream.ReadAsync(buffer, 0, buffer.Length);
string text = Encoding.Default.GetString(buffer);
2023-12-02 10:45:25 +04:00
using (FileStream targetStream = new FileStream("/var/result/data.txt", FileMode.OpenOrCreate))
{
targetStream.SetLength(0);
byte[] textBytes = Encoding.Default.GetBytes(text);
await targetStream.WriteAsync(textBytes, 0, textBytes.Length);
}
}