Files
markus/src/Services/FileService.cs
2026-01-15 11:11:42 -06:00

52 lines
1.3 KiB
C#

using System.IO;
using System.Threading.Tasks;
namespace MarkdownEditor.Services;
/// <summary>
/// Default implementation of <see cref="IFileService"/> using System.IO.
/// Provides file system operations for reading and writing markdown files.
/// </summary>
public class FileService : IFileService
{
/// <inheritdoc/>
public async Task<string> ReadFileAsync(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new System.ArgumentException("File path cannot be null or empty.", nameof(path));
}
return await File.ReadAllTextAsync(path);
}
/// <inheritdoc/>
public async Task WriteFileAsync(string path, string content)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new System.ArgumentException("File path cannot be null or empty.", nameof(path));
}
await File.WriteAllTextAsync(path, content ?? string.Empty);
}
/// <inheritdoc/>
public bool FileExists(string path)
{
return !string.IsNullOrWhiteSpace(path) && File.Exists(path);
}
/// <inheritdoc/>
public string GetFileName(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return string.Empty;
}
return Path.GetFileName(path);
}
}