123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- 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
- {
- /// <summary>
- /// Save file
- /// </summary>
- 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);
- }
- /// <summary>
- /// Write content to file.
- /// </summary>
- /// <param name="path">The file path.</param>
- /// <param name="content">The content.</param>
- /// <param name="encoding">The encoding type.</param>
- /// <param name="mode">The file mode.</param>
- /// <param name="fileShare">The file share mode.</param>
- 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);
- }
- }
- /// <summary>
- /// Write string content to file.
- /// </summary>
- /// <param name="filePath">The file path.</param>
- /// <param name="content">The string contnet.</param>
- public static void WriteFile(string filePath, string content)
- {
- using (var sw = new StreamWriter(filePath, false))
- {
- sw.Write(content);
- }
- }
- /// <summary>
- /// Write bytes to file.
- /// </summary>
- /// <param name="path">The file path.</param>
- /// <param name="content">The bytes content.</param>
- 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);
- }
- }
- /// <summary>
- /// Read file with string content.
- /// </summary>
- /// <param name="filePath">The file path.</param>
- /// <returns>The</returns>
- public static string ReadStringFromFile(string filePath)
- {
- return ReadStringFromFile(filePath, Encoding.Default);
- }
- /// <summary>
- /// Read file with string content.
- /// </summary>
- /// <param name="filePath">The file path.</param>
- /// <returns></returns>
- public static string ReadStringFromFile(string filePath, Encoding encoding)
- {
- return encoding.GetString(ReadBytesFromFile(filePath));
- }
- /// <summary>
- /// Read bytes from file.
- /// </summary>
- /// <param name="filePath">The file path</param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// Gets File Size (B)
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public static long GetFileSizeB(string filePath)
- {
- FileInfo fileInfo = new FileInfo(filePath);
- return fileInfo.Length % 1024;
- }
- /// <summary>
- /// Gets File Size (KB)
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public static long GetFileSizeKB(string filePath)
- {
- FileInfo fileInfo = new FileInfo(filePath);
- return fileInfo.Length / (1024 * 1024);
- }
- /// <summary>
- /// Gets File Size (MB)
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Cope file.
- /// </summary>
- /// <param name="sourceFilePath">The source file.</param>
- /// <param name="destFilePath">The destination file.</param>
- 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}");
- }
- }
- /// <summary>
- /// Cope file.
- /// </summary>
- /// <param name="sourceFilePath">The source file.</param>
- /// <param name="destFilePath">The destination file.</param>
- 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<FileInfo> 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];
- }
- /// <summary>
- /// Delete file
- /// </summary>
- /// <param name="filePath">The file to be deleted.</param>
- 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}");
- }
- }
- }
- }
|