Innovenergy_trunk/csharp/Lib/SysTools/Utils/Utils.cs

37 lines
1.1 KiB
C#

namespace InnovEnergy.Lib.SysTools.Utils;
public static class Utils
{
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static DateTime FromUnixTime(UInt64 unixTime)
{
return Epoch.AddSeconds(unixTime);
}
public static R ValueOrDefault<T, R>(this Dictionary<T, R> dict, T key)
{
return ValueOrDefault(dict, key, default);
}
public static R ValueOrDefault<T, R>(this Dictionary<T, R> dict, T key, R defaultValue)
{
return dict.TryGetValue(key, out var value) ? value : defaultValue;
}
public static void CopyFilesRecursively(String source, String target)
{
CopyFilesRecursively(new DirectoryInfo(source), new DirectoryInfo(target));
}
public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
{
foreach (var file in source.GetFiles())
file.CopyTo(Path.Combine(target.FullName, file.Name));
foreach (var dir in source.GetDirectories())
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
}
}