44 lines
1.3 KiB
C#
Raw Normal View History

2021-08-23 16:57:25 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
2021-09-25 09:23:50 +08:00
namespace Infrastructure
2021-08-23 16:57:25 +08:00
{
public class FileUtil
{
/// <summary>
/// 按时间来创建文件夹
/// </summary>
/// <param name="path"></param>
2021-12-16 11:29:03 +08:00
/// <returns>eg: /{yourPath}/2020/11/3/</returns>
2021-08-23 16:57:25 +08:00
public static string GetdirPath(string path = "")
{
DateTime date = DateTime.Now;
string timeDir = Path.Combine(date.ToString("yyyyMMdd"));// date.ToString("yyyyMM/dd/HH/");
2021-08-23 16:57:25 +08:00
if (!string.IsNullOrEmpty(path))
{
timeDir = Path.Combine(path, timeDir);
2021-08-23 16:57:25 +08:00
}
return timeDir;
}
/// <summary>
/// 取文件名的MD5值(16位)
/// </summary>
/// <param name="name">文件名,不包括扩展名</param>
/// <returns></returns>
public static string HashFileName(string str = null)
{
if (string.IsNullOrEmpty(str))
{
str = Guid.NewGuid().ToString();
}
MD5CryptoServiceProvider md5 = new();
2021-08-23 16:57:25 +08:00
return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
}
}
}