using System;
using LessIO.Strategies;
using LessIO.Strategies.Win32;
using BadPath = System.IO.Path;
using System.Collections.Generic;
namespace LessIO
{
///
/// Provides various file system operations for the current platform.
///
public static class FileSystem
{
private static readonly Lazy LazyStrategy = new Lazy(() => new Win32FileSystemStrategy());
private static FileSystemStrategy Strategy
{
get { return LazyStrategy.Value; }
}
///
/// Sets the date and time that the specified file was last written to.
///
/// The path of the file to set the file time on.
///
/// The date to set for the last write date and time of specified file.
/// Expressed in local time.
///
public static void SetLastWriteTime(Path path, DateTime lastWriteTime)
{
Strategy.SetLastWriteTime(path, lastWriteTime);
}
public static void SetAttributes(Path path, LessIO.FileAttributes fileAttributes)
{
Strategy.SetAttributes(path, fileAttributes);
}
public static LessIO.FileAttributes GetAttributes(Path path)
{
return Strategy.GetAttributes(path);
}
///
/// Returns true if a file or directory exists at the specified path.
///
public static bool Exists(Path path)
{
return Strategy.Exists(path);
}
///
/// Creates the specified directory.
///
///
/// Creates parent directories as needed.
///
public static void CreateDirectory(Path path)
{
Strategy.CreateDirectory(path);
}
///
/// Removes/deletes an existing empty directory.
/// See https://msdn.microsoft.com/en-us/library/windows/desktop/aa365488%28v=vs.85%29.aspx
///
///
public static void RemoveDirectory(Path path)
{
RemoveDirectory(path, false);
}
///
/// Removes/deletes an existing directory.
/// See https://msdn.microsoft.com/en-us/library/windows/desktop/aa365488%28v=vs.85%29.aspx
///
/// The path to the directory to remove.
///
/// True to remove the directory and all of its contents recursively.
/// False will remove the directory only if it is empty.
///
/// Recursively implies removing contained files forcefully.
public static void RemoveDirectory(Path path, bool recursively)
{
Strategy.RemoveDirectory(path, recursively);
}
///
/// Removes/deletes an existing file.
/// To remove a directory see .
/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363915%28v=vs.85%29.aspx
///
/// The file to remove.
/// True to remove the file even if it is read-only.
public static void RemoveFile(Path path, bool forcefully)
{
Strategy.RemoveFile(path, forcefully);
}
///
/// Removes/deletes an existing file.
/// To remove a directory see .
/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363915%28v=vs.85%29.aspx
///
/// The file to remove.
public static void RemoveFile(Path path)
{
Strategy.RemoveFile(path, false);
}
///
/// Copies the specified existing file to a new location.
/// Will throw an exception if the destination file already exists.
///
public static void Copy(Path source, Path dest)
{
if (!Strategy.Exists(source))
throw new Exception(string.Format("The file \"{0}\" does not exist.", source));
Strategy.Copy(source, dest);
}
///
/// Returns true if the specified path is a directory.
///
internal static bool IsDirectory(Path path)
{
return Strategy.IsDirectory(path);
}
///
/// Creates or overwrites the file at the specified path.
///
/// The path and name of the file to create. Supports long file paths.
/// A that provides read/write access to the file specified in path.
public static System.IO.Stream CreateFile(Path path)
{
return Strategy.CreateFile(path);
}
///
/// Returns the contents of the specified directory.
///
/// The path to the directory to get the contents of.
public static IEnumerable ListContents(Path directory)
{
return Strategy.ListContents(directory);
}
///
/// Returns the contents of the specified directory.
///
/// The path to the directory to get the contents of.
/// True to list the contents of any child directories.
/// If the specified directory is not actually a directory then an empty set is returned.
public static IEnumerable ListContents(Path directory, bool recursive)
{
return Strategy.ListContents(directory, recursive);
}
}
}