123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- namespace Aip.Service.Utils;
- public static class FileUtils
- {
- public static string GetFileData(string fileName)
- {
- string result = string.Empty;
- try
- {
- if (File.Exists(fileName))
- {
- byte[] fileBytes = File.ReadAllBytes(fileName);
- result = Convert.ToBase64String(fileBytes);
- }
- }
- catch (Exception)
- {
- return string.Empty;
- }
- return result;
- }
- public static void DeleteFile(string fileName)
- {
- try
- {
- if (File.Exists(fileName))
- {
- //File.Delete(fileName);
- }
- }
- catch (Exception) { }
- }
- public static string FileSizeFormatBytes(long bytes)
- {
- const int scale = 1024;
- string[] orders = new string[] { "GB", "MB", "KB", "Bytes" };
- long max = (long)Math.Pow(scale, orders.Length - 1);
- foreach (string order in orders)
- {
- if (bytes > max)
- return string.Format("{0:##.##} {1}", decimal.Divide(bytes, max), order);
- max /= scale;
- }
- return "0 Bytes";
- }
- public static bool IsExists(string fileName)
- {
- return File.Exists(fileName);
- }
- public static string GetFileName(string fileName)
- {
- return Path.GetFileName(fileName);
- }
- public static string GetFileNameWithoutExtension(string fileName)
- {
- return Path.GetFileNameWithoutExtension(fileName);
- }
- public static string GetExtension(string fileName)
- {
- string fileExt = Path.GetExtension(fileName).ToLower();
- if (fileExt == null)
- {
- return ".";
- }
- return fileExt;
- }
- }
|