< Summary

Information
Class: MyNet.IO.FileHelper
Assembly: MyNet.IO
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.IO/FileHelper.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 98
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
EnsureDirectoryExists(...)100%22100%
EnsureFileExists(...)100%44100%
TryDeleteFile(...)100%22100%
TryCreateFile(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.IO/FileHelper.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="FileHelper.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.IO;
 9
 10namespace MyNet.IO;
 11
 12/// <summary>
 13/// Provides utility methods for common file-system operations.
 14/// </summary>
 15public 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    {
 3030        ArgumentException.ThrowIfNullOrWhiteSpace(path);
 31
 3032        if (File.Exists(path))
 33        {
 334            throw new IOException($"Cannot create directory '{path}' because a file already exists at this location.");
 35        }
 36
 2737        _ = Directory.CreateDirectory(path);
 2738    }
 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)
 2152        => string.IsNullOrWhiteSpace(filename)
 2153            ? throw new InvalidOperationException("No source filename has been configured.")
 2154            : !File.Exists(filename)
 2155                ? throw new FileNotFoundException("The specified items file was not found.", filename)
 2156                : 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    {
 668        ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
 69
 670        if (!File.Exists(filePath))
 71        {
 372            return false;
 73        }
 74
 375        File.Delete(filePath);
 76
 377        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        {
 1589            using var stream = new FileStream(path, FileMode.CreateNew);
 990            return true;
 91        }
 692        catch (IOException)
 93        {
 694            return false;
 95        }
 1596    }
 97}
 98