63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace EFCDesk.Classes
|
|
{
|
|
public static class FileUtil
|
|
{
|
|
public static bool IsFileReady(string path)
|
|
{
|
|
try
|
|
{
|
|
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
|
|
return stream.Length >= 0; // si abre sin excepción, está listo
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool WaitAllFilesReady(IEnumerable<string> files, int perFileTimeoutSeconds)
|
|
{
|
|
var deadline = DateTime.UtcNow.AddSeconds(perFileTimeoutSeconds);
|
|
var pendientes = new HashSet<string>(files);
|
|
|
|
while (pendientes.Count > 0 && DateTime.UtcNow < deadline)
|
|
{
|
|
var listos = new List<string>();
|
|
foreach (var f in pendientes)
|
|
{
|
|
if (File.Exists(f) && IsFileReady(f))
|
|
listos.Add(f);
|
|
}
|
|
foreach (var ok in listos)
|
|
pendientes.Remove(ok);
|
|
|
|
if (pendientes.Count == 0) return true;
|
|
Thread.Sleep(500);
|
|
}
|
|
return pendientes.Count == 0;
|
|
}
|
|
|
|
public static string? TryGetSHA1(string path)
|
|
{
|
|
try
|
|
{
|
|
using var fs = File.OpenRead(path);
|
|
using var sha1 = SHA1.Create();
|
|
var hash = sha1.ComputeHash(fs);
|
|
return BitConverter.ToString(hash).Replace("-", "");
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|