FileUtility.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. namespace NRKernal.Release
  5. {
  6. public class FileUtility
  7. {
  8. public static void SaveStringFile(string path, string content)
  9. {
  10. EnsureFolder(path);
  11. FileStream fs = File.OpenWrite(path);
  12. fs.SetLength(0);
  13. StreamWriter sw = new StreamWriter(fs);
  14. sw.Write(content);
  15. sw.Dispose();
  16. fs.Dispose();
  17. }
  18. public static void SaveFileByteArray(string path, byte[] bytes)
  19. {
  20. EnsureFolder(path);
  21. FileStream fs = File.OpenWrite(path);
  22. fs.Write(bytes, 0, bytes.Length);
  23. fs.Dispose();
  24. }
  25. public static void EnsureFolder(string path)
  26. {
  27. string folder = Path.GetDirectoryName(path);
  28. if (!Directory.Exists(folder))
  29. {
  30. Directory.CreateDirectory(folder);
  31. }
  32. }
  33. public static bool IsDir(string path)
  34. {
  35. var attr = File.GetAttributes(path);
  36. bool isDir = ((attr & FileAttributes.Directory) == FileAttributes.Directory);
  37. return isDir;
  38. }
  39. public static string LoadStringFile(string path)
  40. {
  41. if (path.Contains("://"))
  42. {
  43. using (WWW www = new WWW(path))
  44. {
  45. while (!www.isDone) { }
  46. if (!string.IsNullOrEmpty(www.error))
  47. {
  48. Debug.LogError(www.error);
  49. }
  50. else
  51. {
  52. return www.text;
  53. }
  54. }
  55. }
  56. else
  57. {
  58. if (File.Exists(path))
  59. {
  60. using (StreamReader sr = File.OpenText(path))
  61. {
  62. string content = sr.ReadToEnd();
  63. sr.Dispose();
  64. return content;
  65. }
  66. }
  67. }
  68. return null;
  69. }
  70. public static byte[] LoadFileByteArray(string path)
  71. {
  72. if (path.Contains("://"))
  73. {
  74. using (WWW www = new WWW(path))
  75. {
  76. while (!www.isDone) { }
  77. if (!string.IsNullOrEmpty(www.error))
  78. {
  79. Debug.LogError(www.error);
  80. }
  81. else
  82. {
  83. return www.bytes;
  84. }
  85. }
  86. }
  87. else
  88. {
  89. if (!File.Exists(path))
  90. {
  91. return null;
  92. }
  93. using (FileStream fs = File.OpenRead(path))
  94. {
  95. byte[] bytes = new byte[fs.Length];
  96. fs.Read(bytes, 0, bytes.Length);
  97. fs.Close();
  98. return bytes;
  99. }
  100. }
  101. return null;
  102. }
  103. public static DirectoryInfo WalkDirectoryTree(DirectoryInfo root, Func<DirectoryInfo, bool> itFunc)
  104. {
  105. DirectoryInfo[] subDirs = null;
  106. if (itFunc.Invoke(root))
  107. return root;
  108. subDirs = root.GetDirectories();
  109. foreach (System.IO.DirectoryInfo dirInfo in subDirs)
  110. {
  111. // Resursive call for each subdirectory.
  112. var result = WalkDirectoryTree(dirInfo, itFunc);
  113. if (result != null)
  114. {
  115. return result;
  116. }
  117. }
  118. return null;
  119. }
  120. public static void DeleteDirectory(string target_dir)
  121. {
  122. string[] files = Directory.GetFiles(target_dir);
  123. string[] dirs = Directory.GetDirectories(target_dir);
  124. foreach (string file in files)
  125. {
  126. File.SetAttributes(file, FileAttributes.Normal);
  127. File.Delete(file);
  128. }
  129. foreach (string dir in dirs)
  130. {
  131. DeleteDirectory(dir);
  132. }
  133. Directory.Delete(target_dir, false);
  134. }
  135. }
  136. }