/// <summary> /// Attempts to read the text contained in the file /// </summary> /// <returns></returns> public static Option <string> TryReadAllText(this IFilePath path) => path.IsUnspecified() ? some("") : Try(() => { if (path.Exists()) { return(path.ReadAllText()); } throw new FileNotFoundException($"The file {path.FileSystemPath} could not be found"); });
/// <summary> /// Attempts to read the text contained in the file /// </summary> /// <returns></returns> public static Option <string> TryReadAllText(this IFilePath path) => Try(() => { if (path.Exists()) { return(File.ReadAllText(path.FileSystemPath)); } throw new FileNotFoundException($"The file {path.FileSystemPath} could not be found"); });
/// <summary> /// Copies the represented file to a specified folder /// </summary> /// <param name="dstfolder">The target folder</param> /// <param name="overwrite"></param> /// <param name="createFolder">Specifies whether to create the folder if it doesn't exist</param> /// <returns></returns> public static FileCopyResult CopyTo(this IFilePath path, FolderPath dstfolder, bool overwrite = true, bool createFolder = true) { if (createFolder) { dstfolder.CreateIfMissing(); } var dstpath = dstfolder + path.FileName; try { var exists = path.Exists(); File.Copy(path.FileSystemPath, dstpath, overwrite); return(new FileCopyResult(path.FileSystemPath, dstpath, exists && overwrite)); } catch (Exception e) { return(new FileCopyResult(path.FileSystemPath, dstpath, e.Message)); } }