You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

283 lines
9.9 KiB

  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Text;
  4. using UnifiedIO.SystemIO.Internal;
  5. namespace UnifiedIO
  6. {
  7. public static class File
  8. {
  9. public static bool Exists(string path)
  10. {
  11. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  12. return System.IO.File.Exists(path);
  13. }
  14. public static void CreateEmpty(string path)
  15. {
  16. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  17. string directoryName = System.IO.Path.GetDirectoryName(path);
  18. if (System.IO.Directory.Exists(directoryName))
  19. {
  20. System.IO.File.Create(path).Dispose();
  21. return;
  22. }
  23. FileNotFoundException ex = new FileNotFoundException("Unable to find directory: " + directoryName);
  24. throw ex;
  25. }
  26. public static void Rename(string path, string newName)
  27. {
  28. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  29. if (newName.Contains("/") || newName.Contains("\\"))
  30. {
  31. throw new FileNotFoundException();
  32. }
  33. string directoryName = System.IO.Path.GetDirectoryName(path);
  34. try
  35. {
  36. System.IO.File.Move(path, System.IO.Path.Combine(directoryName, newName));
  37. }
  38. catch (DirectoryNotFoundException)
  39. {
  40. throw new FileNotFoundException();
  41. }
  42. }
  43. public static void Move(string path, string destinationPath)
  44. {
  45. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  46. destinationPath = UnifiedIO.SystemIO.Internal.Path.GetFullPath(destinationPath);
  47. if (System.IO.File.Exists(destinationPath))
  48. {
  49. System.IO.File.Delete(destinationPath);
  50. }
  51. System.IO.File.Move(path, destinationPath);
  52. }
  53. public static void Copy(string path, string destinationPath)
  54. {
  55. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  56. destinationPath = UnifiedIO.SystemIO.Internal.Path.GetFullPath(destinationPath);
  57. if (System.IO.File.Exists(destinationPath))
  58. {
  59. System.IO.File.Delete(destinationPath);
  60. }
  61. System.IO.File.Copy(path, destinationPath);
  62. }
  63. public static void MoveInside(string path, string destinationPath)
  64. {
  65. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  66. destinationPath = UnifiedIO.SystemIO.Internal.Path.GetFullPath(destinationPath);
  67. string fileName = System.IO.Path.GetFileName(path);
  68. string text = System.IO.Path.Combine(destinationPath, fileName);
  69. if (System.IO.File.Exists(text))
  70. {
  71. System.IO.File.Delete(text);
  72. }
  73. System.IO.File.Move(path, text);
  74. }
  75. public static void CopyInside(string path, string destinationPath)
  76. {
  77. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  78. destinationPath = UnifiedIO.SystemIO.Internal.Path.GetFullPath(destinationPath);
  79. string fileName = System.IO.Path.GetFileName(path);
  80. string text = System.IO.Path.Combine(destinationPath, fileName);
  81. if (System.IO.File.Exists(text))
  82. {
  83. System.IO.File.Delete(text);
  84. }
  85. System.IO.File.Copy(path, text);
  86. }
  87. public static void Delete(string path)
  88. {
  89. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  90. if (System.IO.File.Exists(path))
  91. {
  92. System.IO.File.Delete(path);
  93. return;
  94. }
  95. FileNotFoundException ex = new FileNotFoundException("Unable to find the specified file: " + path);
  96. throw ex;
  97. }
  98. public static Stream GetReadStream(string path)
  99. {
  100. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  101. try
  102. {
  103. return System.IO.File.OpenRead(path);
  104. }
  105. catch (DirectoryNotFoundException)
  106. {
  107. throw new FileNotFoundException();
  108. }
  109. }
  110. public static Stream GetWriteStream(string path)
  111. {
  112. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  113. try
  114. {
  115. return System.IO.File.Create(path);
  116. }
  117. catch (DirectoryNotFoundException)
  118. {
  119. throw new FileNotFoundException();
  120. }
  121. }
  122. public static Stream GetAppendStream(string path)
  123. {
  124. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  125. try
  126. {
  127. return System.IO.File.Open(path, FileMode.Append);
  128. }
  129. catch (DirectoryNotFoundException)
  130. {
  131. throw new FileNotFoundException();
  132. }
  133. }
  134. public static byte[] ReadBytes(string path, int position = 0, int nBytes = 0)
  135. {
  136. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  137. byte[] array = null;
  138. using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
  139. {
  140. if (position > 0)
  141. {
  142. fileStream.Position = position;
  143. }
  144. int num = (int)(fileStream.Length - fileStream.Position);
  145. if (nBytes == 0 || nBytes > num)
  146. {
  147. nBytes = num;
  148. }
  149. if (nBytes < 0)
  150. {
  151. nBytes = 0;
  152. }
  153. array = new byte[nBytes];
  154. readStreamToBuffer(fileStream, array);
  155. return array;
  156. }
  157. }
  158. public static void WriteBytes(string path, byte[] content)
  159. {
  160. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  161. using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
  162. {
  163. fileStream.Write(content, 0, content.Length);
  164. }
  165. }
  166. public static void WriteBytes(string path, byte[] content, int position)
  167. {
  168. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  169. using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
  170. {
  171. if (position > 0)
  172. {
  173. fileStream.Position = position;
  174. }
  175. fileStream.Write(content, 0, content.Length);
  176. }
  177. }
  178. public static void AppendBytes(string path, byte[] bytes)
  179. {
  180. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  181. using (FileStream fileStream = new FileStream(path, FileMode.Append, FileAccess.Write))
  182. {
  183. fileStream.Write(bytes, 0, bytes.Length);
  184. }
  185. }
  186. public static string ReadText(string path)
  187. {
  188. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  189. using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
  190. {
  191. return streamReader.ReadToEnd();
  192. }
  193. }
  194. public static void WriteText(string path, string content)
  195. {
  196. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  197. using (StreamWriter streamWriter = new StreamWriter(path, false, Encoding.UTF8))
  198. {
  199. streamWriter.Write(content);
  200. }
  201. }
  202. public static void AppendText(string path, string content)
  203. {
  204. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  205. using (StreamWriter streamWriter = new StreamWriter(path, true, Encoding.UTF8))
  206. {
  207. streamWriter.Write(content);
  208. }
  209. }
  210. public static IList<string> ReadLines(string path)
  211. {
  212. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  213. List<string> list = new List<string>();
  214. using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
  215. {
  216. string item;
  217. while ((item = streamReader.ReadLine()) != null)
  218. {
  219. list.Add(item);
  220. }
  221. }
  222. return list.ToArray();
  223. }
  224. public static void WriteLines(string path, IEnumerable<string> content)
  225. {
  226. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  227. using (StreamWriter streamWriter = new StreamWriter(path, false, Encoding.UTF8))
  228. {
  229. foreach (string item in content)
  230. {
  231. streamWriter.WriteLine(item);
  232. }
  233. }
  234. }
  235. public static void AppendLines(string path, IEnumerable<string> content)
  236. {
  237. path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
  238. using (StreamWriter streamWriter = new StreamWriter(path, true, Encoding.UTF8))
  239. {
  240. foreach (string item in content)
  241. {
  242. streamWriter.WriteLine(item);
  243. }
  244. }
  245. }
  246. private static int readStreamToBuffer(FileStream fs, byte[] buffer)
  247. {
  248. int num = 0;
  249. int num2 = buffer.Length;
  250. do
  251. {
  252. int num3 = fs.Read(buffer, num, num2);
  253. num += num3;
  254. num2 -= num3;
  255. }
  256. while (num2 > 0 && fs.Position < fs.Length);
  257. return num;
  258. }
  259. }
  260. }