using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Vinno.IUS.Common.Log;
namespace Vinno.FIS.Sonopost.Helpers
{
public class FileHelper
{
///
/// Save file
///
public static void Save(string path, string content)
{
var data = new UTF8Encoding().GetBytes(content);
using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
stream.Write(data, 0, data.Length);
}
}
public static string GetText(string path)
{
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))//Felix:若改为ReadWrite,SmartLogs会提示文件被占用。
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
string text = Encoding.UTF8.GetString(buffer);
return text;
}
}
public static void CopyStream(string path, Stream destStream)
{
using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
fs.CopyTo(destStream);
}
}
public static bool Exist(string path)
{
return File.Exists(path);
}
///
/// Write content to file.
///
/// The file path.
/// The content.
/// The encoding type.
/// The file mode.
/// The file share mode.
public static void WriteFile(string path, string content, Encoding encoding, FileMode mode = FileMode.Create, FileShare fileShare = FileShare.None)
{
using (FileStream fs = new FileStream(path, mode, FileAccess.Write, fileShare))
{
byte[] bytes = encoding.GetBytes(content);
fs.Write(bytes, 0, bytes.Length);
}
}
///
/// Write string content to file.
///
/// The file path.
/// The string contnet.
public static void WriteFile(string filePath, string content)
{
using (var sw = new StreamWriter(filePath, false))
{
sw.Write(content);
}
}
///
/// Write bytes to file.
///
/// The file path.
/// The bytes content.
public static void WriteFile(string path, byte[] bytes)
{
using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.None))
{
fs.Write(bytes, 0, bytes.Length);
}
}
///
/// Read file with string content.
///
/// The file path.
/// The
public static string ReadStringFromFile(string filePath)
{
return ReadStringFromFile(filePath, Encoding.Default);
}
///
/// Read file with string content.
///
/// The file path.
///
public static string ReadStringFromFile(string filePath, Encoding encoding)
{
return encoding.GetString(ReadBytesFromFile(filePath));
}
///
/// Read bytes from file.
///
/// The file path
///
public static byte[] ReadBytesFromFile(string filePath)
{
if (File.Exists(filePath))
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte[] buffur = new byte[fs.Length];
fs.Read(buffur, 0, (int)fs.Length);
return buffur;
}
}
else
{
return null;
}
}
///
/// Gets File Size (B)
///
///
///
public static long GetFileSizeB(string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length % 1024;
}
///
/// Gets File Size (KB)
///
///
///
public static long GetFileSizeKB(string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length / (1024 * 1024);
}
///
/// Gets File Size (MB)
///
///
///
public static long GetFileSizeMB(string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length / (1024 * 1024 * 1024);
}
public static string GetFileSizeString(double length)
{
string[] sizes = { "B", "KB", "MB", "GB" };
int order = 0;
while (length >= 1024 && (order + 1) < sizes.Length)
{
order++;
length /= 1024;
}
string filesize = string.Format("{0:0.##} {1}", length, sizes[order]);
return filesize;
}
///
/// Cope file.
///
/// The source file.
/// The destination file.
public static void CopyFile(string sourceFilePath, string destFilePath, bool overwrite)
{
try
{
if (File.Exists(sourceFilePath))
{
var newDirectory = new FileInfo(destFilePath).DirectoryName;
DirectoryHelper.CreateDirectory(newDirectory);
File.Copy(sourceFilePath, destFilePath, overwrite);
}
}
catch (Exception ex)
{
Logger.WriteLineError($"Copy file {sourceFilePath} to {destFilePath} error,detail:{ex}");
}
}
///
/// Cope file.
///
/// The source file.
/// The destination file.
public static void MoveFile(string sourceFilePath, string destFilePath, bool overwrite)
{
try
{
if (File.Exists(sourceFilePath))
{
var newDirectory = new FileInfo(destFilePath).DirectoryName;
DirectoryHelper.CreateDirectory(newDirectory);
File.Copy(sourceFilePath, destFilePath, overwrite);
DeleteFile(sourceFilePath);
}
}
catch (Exception ex)
{
Logger.WriteLineError($"Copy file {sourceFilePath} to {destFilePath} error,detail:{ex}");
}
}
public static IEnumerable GetFilesByExtension(string folderPath, string extension)
{
if (Directory.Exists(folderPath))
{
var uploadReportDir = new DirectoryInfo(folderPath);
return uploadReportDir.GetFiles().Where(f => f.Extension == extension);
}
return new FileInfo[0];
}
///
/// Delete file
///
/// The file to be deleted.
public static void DeleteFile(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return;
}
try
{
if (File.Exists(filePath))
{
File.SetAttributes(filePath, FileAttributes.Normal);
File.Delete(filePath);
}
}
catch (Exception ex)
{
Logger.WriteLineError($"Exception when delete file {filePath},detail:{ex}");
}
}
}
}