BinaryFileTool.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2018.3.27 - 2018.6.3
  4. // desc: 工具描述
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using Azylee.Core.IOUtils.DirUtils;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. namespace Azylee.Core.IOUtils.BinaryUtils
  14. {
  15. public static class BinaryFileTool
  16. {
  17. public static bool write(string file, byte[] bytes)
  18. {
  19. try
  20. {
  21. DirTool.Create(Path.GetDirectoryName(file));
  22. //创建一个文件流
  23. using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate))
  24. {
  25. //将byte数组写入文件中
  26. fs.Write(bytes, 0, bytes.Length);
  27. }
  28. return true;
  29. }
  30. catch { }
  31. return false;
  32. }
  33. public static byte[] read(string file)
  34. {
  35. try
  36. {
  37. using (FileStream fs = new FileStream(file, FileMode.Open))
  38. {
  39. long size = fs.Length; //获取文件大小
  40. byte[] array = new byte[size];
  41. fs.Read(array, 0, array.Length); //将文件读到byte数组中
  42. return array;
  43. }
  44. }
  45. catch { }
  46. return null;
  47. }
  48. }
  49. }