|
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using UnifiedIO.SystemIO.Internal;
-
- namespace UnifiedIO
- {
- public static class File
- {
- public static bool Exists(string path)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- return System.IO.File.Exists(path);
- }
-
- public static void CreateEmpty(string path)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- string directoryName = System.IO.Path.GetDirectoryName(path);
- if (System.IO.Directory.Exists(directoryName))
- {
- System.IO.File.Create(path).Dispose();
- return;
- }
- FileNotFoundException ex = new FileNotFoundException("Unable to find directory: " + directoryName);
- throw ex;
- }
-
- public static void Rename(string path, string newName)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- if (newName.Contains("/") || newName.Contains("\\"))
- {
- throw new FileNotFoundException();
- }
- string directoryName = System.IO.Path.GetDirectoryName(path);
- try
- {
- System.IO.File.Move(path, System.IO.Path.Combine(directoryName, newName));
- }
- catch (DirectoryNotFoundException)
- {
- throw new FileNotFoundException();
- }
- }
-
- public static void Move(string path, string destinationPath)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- destinationPath = UnifiedIO.SystemIO.Internal.Path.GetFullPath(destinationPath);
- if (System.IO.File.Exists(destinationPath))
- {
- System.IO.File.Delete(destinationPath);
- }
- System.IO.File.Move(path, destinationPath);
- }
-
- public static void Copy(string path, string destinationPath)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- destinationPath = UnifiedIO.SystemIO.Internal.Path.GetFullPath(destinationPath);
- if (System.IO.File.Exists(destinationPath))
- {
- System.IO.File.Delete(destinationPath);
- }
- System.IO.File.Copy(path, destinationPath);
- }
-
- public static void MoveInside(string path, string destinationPath)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- destinationPath = UnifiedIO.SystemIO.Internal.Path.GetFullPath(destinationPath);
- string fileName = System.IO.Path.GetFileName(path);
- string text = System.IO.Path.Combine(destinationPath, fileName);
- if (System.IO.File.Exists(text))
- {
- System.IO.File.Delete(text);
- }
- System.IO.File.Move(path, text);
- }
-
- public static void CopyInside(string path, string destinationPath)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- destinationPath = UnifiedIO.SystemIO.Internal.Path.GetFullPath(destinationPath);
- string fileName = System.IO.Path.GetFileName(path);
- string text = System.IO.Path.Combine(destinationPath, fileName);
- if (System.IO.File.Exists(text))
- {
- System.IO.File.Delete(text);
- }
- System.IO.File.Copy(path, text);
- }
-
- public static void Delete(string path)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- if (System.IO.File.Exists(path))
- {
- System.IO.File.Delete(path);
- return;
- }
- FileNotFoundException ex = new FileNotFoundException("Unable to find the specified file: " + path);
- throw ex;
- }
-
- public static Stream GetReadStream(string path)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- try
- {
- return System.IO.File.OpenRead(path);
- }
- catch (DirectoryNotFoundException)
- {
- throw new FileNotFoundException();
- }
- }
-
- public static Stream GetWriteStream(string path)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- try
- {
- return System.IO.File.Create(path);
- }
- catch (DirectoryNotFoundException)
- {
- throw new FileNotFoundException();
- }
- }
-
- public static Stream GetAppendStream(string path)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- try
- {
- return System.IO.File.Open(path, FileMode.Append);
- }
- catch (DirectoryNotFoundException)
- {
- throw new FileNotFoundException();
- }
- }
-
- public static byte[] ReadBytes(string path, int position = 0, int nBytes = 0)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- byte[] array = null;
- using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
- {
- if (position > 0)
- {
- fileStream.Position = position;
- }
- int num = (int)(fileStream.Length - fileStream.Position);
- if (nBytes == 0 || nBytes > num)
- {
- nBytes = num;
- }
- if (nBytes < 0)
- {
- nBytes = 0;
- }
- array = new byte[nBytes];
- readStreamToBuffer(fileStream, array);
- return array;
- }
- }
-
- public static void WriteBytes(string path, byte[] content)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
- {
- fileStream.Write(content, 0, content.Length);
- }
- }
-
- public static void WriteBytes(string path, byte[] content, int position)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
- {
- if (position > 0)
- {
- fileStream.Position = position;
- }
- fileStream.Write(content, 0, content.Length);
- }
- }
-
- public static void AppendBytes(string path, byte[] bytes)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- using (FileStream fileStream = new FileStream(path, FileMode.Append, FileAccess.Write))
- {
- fileStream.Write(bytes, 0, bytes.Length);
- }
- }
-
- public static string ReadText(string path)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
- {
- return streamReader.ReadToEnd();
- }
- }
-
- public static void WriteText(string path, string content)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- using (StreamWriter streamWriter = new StreamWriter(path, false, Encoding.UTF8))
- {
- streamWriter.Write(content);
- }
- }
-
- public static void AppendText(string path, string content)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- using (StreamWriter streamWriter = new StreamWriter(path, true, Encoding.UTF8))
- {
- streamWriter.Write(content);
- }
- }
-
- public static IList<string> ReadLines(string path)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- List<string> list = new List<string>();
- using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
- {
- string item;
- while ((item = streamReader.ReadLine()) != null)
- {
- list.Add(item);
- }
- }
- return list.ToArray();
- }
-
- public static void WriteLines(string path, IEnumerable<string> content)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- using (StreamWriter streamWriter = new StreamWriter(path, false, Encoding.UTF8))
- {
- foreach (string item in content)
- {
- streamWriter.WriteLine(item);
- }
- }
- }
-
- public static void AppendLines(string path, IEnumerable<string> content)
- {
- path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(path);
- using (StreamWriter streamWriter = new StreamWriter(path, true, Encoding.UTF8))
- {
- foreach (string item in content)
- {
- streamWriter.WriteLine(item);
- }
- }
- }
-
- private static int readStreamToBuffer(FileStream fs, byte[] buffer)
- {
- int num = 0;
- int num2 = buffer.Length;
- do
- {
- int num3 = fs.Read(buffer, num, num2);
- num += num3;
- num2 -= num3;
- }
- while (num2 > 0 && fs.Position < fs.Length);
- return num;
- }
- }
- }
|