CSharpFileOperator.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.IO;
  2. using System.Text;
  3. namespace XGame.Framework.FileSystem
  4. {
  5. class CSharpFileOperator : IFileOperator
  6. {
  7. public bool Copy(string source, string destination, bool overwrite)
  8. {
  9. System.IO.File.Copy(source, destination, overwrite);
  10. return true;
  11. }
  12. public ICopyFileAsync CopyFileAsync(string source, string destination, bool overwrite)
  13. {
  14. var fileCopyAsync = new CopyFileAsync(source, destination, overwrite);
  15. fileCopyAsync.Start();
  16. return fileCopyAsync;
  17. }
  18. public bool Delete(string fullName)
  19. {
  20. System.IO.File.Delete(fullName);
  21. return true;
  22. }
  23. public bool Exist(string fullName)
  24. {
  25. return System.IO.File.Exists(fullName);
  26. }
  27. public string Extension(string fullName)
  28. {
  29. return Path.GetExtension(fullName);
  30. }
  31. public string GetDirectory(string fullName)
  32. {
  33. return Path.GetDirectoryName(fullName).Replace('\\', '/').Replace("//", "/");
  34. }
  35. public void Move(string source, string destination)
  36. {
  37. System.IO.File.Move(source, destination);
  38. }
  39. public byte[] ReadAllBytes(string filePath)
  40. {
  41. return System.IO.File.ReadAllBytes(filePath);
  42. }
  43. public string ReadAllText(string filePath)
  44. {
  45. byte[] bytes = System.IO.File.ReadAllBytes(filePath);
  46. if (bytes != null && bytes.Length > 0)
  47. return Encoding.UTF8.GetString(bytes);
  48. return string.Empty;
  49. }
  50. public IReadFileAsync ReadFileAsync(string filePath)
  51. {
  52. var fileReadAsync = new ReadFileAsync(filePath);
  53. fileReadAsync.Start();
  54. return fileReadAsync;
  55. }
  56. public void WriteAllBytes(string path, byte[] bytes)
  57. {
  58. System.IO.File.WriteAllBytes(path, bytes);
  59. }
  60. public void WriteAllText(string path, string contents)
  61. {
  62. if (string.IsNullOrEmpty(contents))
  63. {
  64. Log.Warn($"CSharpFileOperator WriteAllText failed. contents is null or empty, {path}");
  65. return;
  66. }
  67. var bytes = Encoding.UTF8.GetBytes(contents);
  68. System.IO.File.WriteAllBytes(path, bytes);
  69. }
  70. public bool Rename(string from, string to)
  71. {
  72. System.IO.File.Move(from, to);
  73. return true;
  74. }
  75. public string GetFileName(string fullName, bool withoutExt = true)
  76. {
  77. if (withoutExt) return Path.GetFileNameWithoutExtension(fullName);
  78. else return Path.GetFileName(fullName);
  79. }
  80. }
  81. }