C#公共类
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.

53 lines
1.3 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Common.Helper.Nlog;
namespace Common.Helper.File
{
public class FileHelper
{
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="file"></param>
public static void CreateFile(string file)
{
if (Directory.Exists(file))
//文件夹已经存在
return;
try
{
Directory.CreateDirectory(file);
//创建成功
}
catch (Exception e)
{
LogHelper.Log.Error($"文件创建出错:{e.ToString()}");
}
}
/// <summary>
/// 获取文件大小
/// </summary>
/// <param name="length">文件长度</param>
/// <returns></returns>
public static string FileSize(long length)
{
var unit = new List<string>() { "K", "M", "G" };
var size = Math.Round(length / 1024.0, 2);
var index = 0;
while (size > 1024 && index < 2)
{
index++;
size = Math.Round(size / 1024, 2);
}
return $"{size}{unit[index]}";
}
}
}