LibHac/LibHac/FileSystem.cs
caitsith2 dec085142b Skip files/directories that cause...
...UnauthorizedAccessException (particularly when the search all starts
from the root directory of an NTFS drive.)
2018-08-31 20:46:28 -05:00

72 lines
2.1 KiB
C#

using System.Collections.Generic;
using System.IO;
namespace LibHac
{
public class FileSystem : IFileSystem
{
public string Root { get; }
public FileSystem(string rootDir)
{
Root = Path.GetFullPath(rootDir);
}
public bool FileExists(string path)
{
return File.Exists(Path.Combine(Root, path));
}
public bool DirectoryExists(string path)
{
return Directory.Exists(Path.Combine(Root, path));
}
public Stream OpenFile(string path, FileMode mode)
{
return new FileStream(Path.Combine(Root, path), mode);
}
public Stream OpenFile(string path, FileMode mode, FileAccess access)
{
return new FileStream(Path.Combine(Root, path), mode, access);
}
public string[] GetFileSystemEntries(string path, string searchPattern)
{
return Directory.GetFileSystemEntries(Path.Combine(Root, path), searchPattern);
}
public string[] GetFileSystemEntries(string path, string searchPattern, SearchOption searchOption)
{
//return Directory.GetFileSystemEntries(Path.Combine(Root, path), searchPattern, searchOption);
var result = new List<string>();
try
{
result.AddRange(GetFileSystemEntries(Path.Combine(Root, path), searchPattern));
}
catch { /**/ }
if (searchOption == SearchOption.TopDirectoryOnly)
return result.ToArray();
var searchDirectories = Directory.GetDirectories(Path.Combine(Root, path));
foreach (var search in searchDirectories)
{
try
{
result.AddRange(GetFileSystemEntries(search, searchPattern, searchOption));
}
catch { /**/ }
}
return result.ToArray();
}
public string GetFullPath(string path)
{
return Path.Combine(Root, path);
}
}
}