PowerToys/Wox.Infrastructure/Helper.cs

60 lines
1.6 KiB
C#
Raw Normal View History

using System;
2016-05-08 02:16:13 +08:00
using System.IO;
namespace Wox.Infrastructure
{
2016-05-08 02:16:13 +08:00
public static class Helper
{
/// <summary>
/// http://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy
/// </summary>
2016-05-08 02:16:13 +08:00
public static T NonNull<T>(this T obj)
{
if (obj == null)
{
throw new NullReferenceException();
}
else
{
return obj;
}
}
2016-05-08 02:16:13 +08:00
public static void RequireNonNull<T>(this T obj)
{
if (obj == null)
{
throw new NullReferenceException();
}
}
public static void ValidateDataDirectory(string bundledDataDirectory, string dataDirectory)
{
if (!Directory.Exists(dataDirectory))
{
Directory.CreateDirectory(dataDirectory);
}
foreach (var bundledDataPath in Directory.GetFiles(bundledDataDirectory))
{
var data = Path.GetFileName(bundledDataPath);
var dataPath = Path.Combine(dataDirectory, data.NonNull());
if (!File.Exists(dataPath))
{
File.Copy(bundledDataPath, dataPath);
}
else
{
var time1 = new FileInfo(bundledDataPath).LastWriteTimeUtc;
var time2 = new FileInfo(dataPath).LastWriteTimeUtc;
if (time1 != time2)
{
File.Copy(bundledDataPath, dataPath, true);
}
}
}
}
}
}