TapFileHelper.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. namespace TapTap.Common.Editor
  6. {
  7. public class TapFileHelper : System.IDisposable
  8. {
  9. private string filePath;
  10. public TapFileHelper(string fPath)
  11. {
  12. filePath = fPath;
  13. if (!System.IO.File.Exists(filePath))
  14. {
  15. Debug.LogError(filePath + "路径下文件不存在");
  16. return;
  17. }
  18. }
  19. public void WriteBelow(string below, string text)
  20. {
  21. StreamReader streamReader = new StreamReader(filePath);
  22. string all = streamReader.ReadToEnd();
  23. streamReader.Close();
  24. int beginIndex = all.IndexOf(below, StringComparison.Ordinal);
  25. if (beginIndex == -1)
  26. {
  27. Debug.LogError(filePath + "中没有找到字符串" + below);
  28. return;
  29. }
  30. int endIndex = all.LastIndexOf("\n", beginIndex + below.Length, StringComparison.Ordinal);
  31. all = all.Substring(0, endIndex) + "\n" + text + "\n" + all.Substring(endIndex);
  32. StreamWriter streamWriter = new StreamWriter(filePath);
  33. streamWriter.Write(all);
  34. streamWriter.Close();
  35. }
  36. public void Replace(string below, string newText)
  37. {
  38. StreamReader streamReader = new StreamReader(filePath);
  39. string all = streamReader.ReadToEnd();
  40. streamReader.Close();
  41. int beginIndex = all.IndexOf(below, StringComparison.Ordinal);
  42. if (beginIndex == -1)
  43. {
  44. Debug.LogError(filePath + "中没有找到字符串" + below);
  45. return;
  46. }
  47. all = all.Replace(below, newText);
  48. StreamWriter streamWriter = new StreamWriter(filePath);
  49. streamWriter.Write(all);
  50. streamWriter.Close();
  51. }
  52. public void Dispose()
  53. {
  54. }
  55. public static void CopyAndReplaceDirectory(string srcPath, string dstPath)
  56. {
  57. if (Directory.Exists(dstPath))
  58. Directory.Delete(dstPath, true);
  59. if (File.Exists(dstPath))
  60. File.Delete(dstPath);
  61. Directory.CreateDirectory(dstPath);
  62. foreach (var file in Directory.GetFiles(srcPath))
  63. File.Copy(file, Path.Combine(dstPath, Path.GetFileName(file)));
  64. foreach (var dir in Directory.GetDirectories(srcPath))
  65. CopyAndReplaceDirectory(dir, Path.Combine(dstPath, Path.GetFileName(dir)));
  66. }
  67. public static void DeleteFileBySuffix(string dir, string[] suffix)
  68. {
  69. if (!Directory.Exists(dir))
  70. {
  71. return;
  72. }
  73. foreach (var file in Directory.GetFiles(dir))
  74. {
  75. foreach (var suffixName in suffix)
  76. {
  77. if (file.Contains(suffixName))
  78. {
  79. File.Delete(file);
  80. }
  81. }
  82. }
  83. }
  84. public static string FilterFileByPrefix(string srcPath, string filterName)
  85. {
  86. if (!Directory.Exists(srcPath))
  87. {
  88. return null;
  89. }
  90. foreach (var dir in Directory.GetDirectories(srcPath))
  91. {
  92. string fileName = Path.GetFileName(dir);
  93. if (fileName.StartsWith(filterName))
  94. {
  95. return Path.Combine(srcPath, Path.GetFileName(dir));
  96. }
  97. }
  98. return null;
  99. }
  100. public static string FilterFileBySuffix(string srcPath, string suffix)
  101. {
  102. if (!Directory.Exists(srcPath))
  103. {
  104. return null;
  105. }
  106. foreach (var dir in Directory.GetDirectories(srcPath))
  107. {
  108. string fileName = Path.GetFileName(dir);
  109. if (fileName.StartsWith(suffix))
  110. {
  111. return Path.Combine(srcPath, Path.GetFileName(dir));
  112. }
  113. }
  114. return null;
  115. }
  116. public static FileInfo RecursionFilterFile(string dir, string fileName)
  117. {
  118. List<FileInfo> fileInfoList = new List<FileInfo>();
  119. Director(dir, fileInfoList);
  120. foreach (FileInfo item in fileInfoList)
  121. {
  122. if (fileName.Equals(item.Name))
  123. {
  124. return item;
  125. }
  126. }
  127. return null;
  128. }
  129. public static void Director(string dir, List<FileInfo> list)
  130. {
  131. DirectoryInfo d = new DirectoryInfo(dir);
  132. FileInfo[] files = d.GetFiles();
  133. DirectoryInfo[] directs = d.GetDirectories();
  134. foreach (FileInfo f in files)
  135. {
  136. list.Add(f);
  137. }
  138. foreach (DirectoryInfo dd in directs)
  139. {
  140. Director(dd.FullName, list);
  141. }
  142. }
  143. }
  144. }