You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.5 KiB
76 lines
2.5 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace UpdateServiceLibrary.Helper
|
|
{
|
|
public class FileController
|
|
{
|
|
/// <summary>
|
|
/// 获取文件哈希值
|
|
/// </summary>
|
|
/// <param name="fileInfo"></param>
|
|
/// <returns></returns>
|
|
public static string GetHashCode(FileInfo fileInfo)
|
|
{
|
|
var hash = SHA1.Create();
|
|
byte[] hashcode;
|
|
using (var fs = new FileStream(fileInfo.FullName, FileMode.Open))
|
|
{
|
|
hashcode = hash.ComputeHash(fs);
|
|
}
|
|
return BitConverter.ToString(hashcode);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 移动文件夹下所有文件与路径
|
|
/// </summary>
|
|
/// <param name="sourcePath"></param>
|
|
/// <param name="destPath"></param>
|
|
public static void MoveFolder(string sourcePath, string destPath)
|
|
{
|
|
if (Directory.Exists(sourcePath))
|
|
{
|
|
if (!Directory.Exists(destPath))
|
|
{
|
|
//目标目录不存在则创建
|
|
try
|
|
{
|
|
Directory.CreateDirectory(destPath);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("创建目标目录失败:" + ex.Message);
|
|
}
|
|
}
|
|
//获得源文件下所有文件
|
|
List<string> files = new List<string>(Directory.GetFiles(sourcePath));
|
|
files.ForEach(c =>
|
|
{
|
|
string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
|
|
//覆盖模式
|
|
if (File.Exists(destFile))
|
|
{
|
|
File.Delete(destFile);
|
|
}
|
|
File.Move(c, destFile);
|
|
});
|
|
//获得源文件下所有目录文件
|
|
List<string> folders = new List<string>(Directory.GetDirectories(sourcePath));
|
|
|
|
folders.ForEach(c =>
|
|
{
|
|
string destDir = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
|
|
//采用递归的方法实现
|
|
MoveFolder(c, destDir);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
throw new DirectoryNotFoundException("源目录不存在!");
|
|
}
|
|
}
|
|
}
|
|
}
|