| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FileHelper.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.IO; |
| | | 9 | | |
| | | 10 | | namespace MyNet.IO; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides utility methods for common file-system operations. |
| | | 14 | | /// </summary> |
| | | 15 | | public static class FileHelper |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Ensures that the specified directory exists. |
| | | 19 | | /// Creates it if it does not already exist. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="path">The directory path.</param> |
| | | 22 | | /// <exception cref="ArgumentException"> |
| | | 23 | | /// Thrown when <paramref name="path"/> is null, empty, or whitespace. |
| | | 24 | | /// </exception> |
| | | 25 | | /// <exception cref="IOException"> |
| | | 26 | | /// Thrown when a file already exists at the specified path. |
| | | 27 | | /// </exception> |
| | | 28 | | public static void EnsureDirectoryExists(string path) |
| | | 29 | | { |
| | 30 | 30 | | ArgumentException.ThrowIfNullOrWhiteSpace(path); |
| | | 31 | | |
| | 30 | 32 | | if (File.Exists(path)) |
| | | 33 | | { |
| | 3 | 34 | | throw new IOException($"Cannot create directory '{path}' because a file already exists at this location."); |
| | | 35 | | } |
| | | 36 | | |
| | 27 | 37 | | _ = Directory.CreateDirectory(path); |
| | 27 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Ensures that the configured filename is valid and accessible. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="filename">The filename to validate.</param> |
| | | 44 | | /// <returns>The validated filename.</returns> |
| | | 45 | | /// <exception cref="InvalidOperationException"> |
| | | 46 | | /// Thrown when no filename has been configured. |
| | | 47 | | /// </exception> |
| | | 48 | | /// <exception cref="FileNotFoundException"> |
| | | 49 | | /// Thrown when the configured file does not exist. |
| | | 50 | | /// </exception> |
| | | 51 | | public static string EnsureFileExists(string filename) |
| | 21 | 52 | | => string.IsNullOrWhiteSpace(filename) |
| | 21 | 53 | | ? throw new InvalidOperationException("No source filename has been configured.") |
| | 21 | 54 | | : !File.Exists(filename) |
| | 21 | 55 | | ? throw new FileNotFoundException("The specified items file was not found.", filename) |
| | 21 | 56 | | : filename; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Deletes the specified file if it exists. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="filePath">The file path.</param> |
| | | 62 | | /// <returns> |
| | | 63 | | /// <c>true</c> if the file existed and was deleted; |
| | | 64 | | /// otherwise, <c>false</c>. |
| | | 65 | | /// </returns> |
| | | 66 | | public static bool TryDeleteFile(string filePath) |
| | | 67 | | { |
| | 6 | 68 | | ArgumentException.ThrowIfNullOrWhiteSpace(filePath); |
| | | 69 | | |
| | 6 | 70 | | if (!File.Exists(filePath)) |
| | | 71 | | { |
| | 3 | 72 | | return false; |
| | | 73 | | } |
| | | 74 | | |
| | 3 | 75 | | File.Delete(filePath); |
| | | 76 | | |
| | 3 | 77 | | return true; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Attempts to create a new empty file at the specified path with exclusive access. Returns <c>true</c> if the file |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="path">The path of the file to create.</param> |
| | | 84 | | /// <returns><c>true</c> if the file was created successfully; otherwise, <c>false</c>.</returns> |
| | | 85 | | public static bool TryCreateFile(string path) |
| | | 86 | | { |
| | | 87 | | try |
| | | 88 | | { |
| | 15 | 89 | | using var stream = new FileStream(path, FileMode.CreateNew); |
| | 9 | 90 | | return true; |
| | | 91 | | } |
| | 6 | 92 | | catch (IOException) |
| | | 93 | | { |
| | 6 | 94 | | return false; |
| | | 95 | | } |
| | 15 | 96 | | } |
| | | 97 | | } |
| | | 98 | | |