Notes/Notes.Desktop/FileService.cs

75 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
namespace Notes.Desktop;
class FileService: IFileService
{
public string Combine(string args0, string args1)
{
return Path.Combine(args0, args1);
}
public string GetRandomFileName()
{
return Path.GetRandomFileName();
}
public IEnumerable<string> EnumerateFiles(string path, string patten)
{
return Directory.EnumerateFiles(path,patten);
}
public string GetFileName(string filename)
{
return Path.GetFileName(filename);
}
public string ReadAllText(string filename)
{
return File.ReadAllText(filename);
}
public DateTime GetLastWriteTime(string filename)
{
return File.GetLastWriteTime(filename);
}
public bool ExistsFile(string filename)
{
return File.Exists(filename);
}
public void Delete(string path)
{
File.Delete(path);
}
public void WriteAllText(string path, string text)
{
File.WriteAllText(path,text);
}
public void EnsureDirectory(string path, bool isFile)
{
var directoryInfo = new DirectoryInfo(path);
Stack<string> paths = new Stack<string>();
if (!isFile)
{
paths.Push(directoryInfo.Name);
}
var directoryInfoParent = directoryInfo.Parent;
while (directoryInfoParent != null && !Directory.Exists(directoryInfoParent.FullName))
{
paths.Push(directoryInfoParent.Name);
directoryInfoParent = directoryInfoParent.Parent;
}
if (directoryInfoParent != null)
while (paths.Count > 0)
{
var pop = paths.Pop();
Directory.CreateDirectory(Path.Combine(directoryInfoParent.FullName, pop));
}
}
}