You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.2 KiB
C#

using System.IO;
using System.Linq;
namespace CommanderApp;
public class FileSystemService : IFileSystemService
{
public string GetRootPath()
{
#if ANDROID || IOS || MACCATALYST
return FileSystem.AppDataDirectory;
#else
var path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
return Path.GetFullPath(path);
#endif
}
public IEnumerable<FileSystemItem> GetDirectoryContents(string path)
{
path = Path.GetFullPath(path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar));
if (!Directory.Exists(path))
{
System.Diagnostics.Debug.WriteLine($"Directory does not exist: {path}");
return Enumerable.Empty<FileSystemItem>();
}
var items = new List<FileSystemItem>();
var parent = Directory.GetParent(path);
if (parent != null)
{
items.Add(new FileSystemItem
{
Name = "..",
FullName = parent.FullName,
IsDirectory = true
});
}
try
{
var dirs = Directory.GetDirectories(path)
.Select(d => new FileSystemItem
{
Name = Path.GetFileName(d),
FullName = d,
IsDirectory = true
});
var files = Directory.GetFiles(path)
.Select(f => new FileSystemItem
{
Name = Path.GetFileName(f),
FullName = f,
IsDirectory = false
});
items.AddRange(dirs.OrderBy(d => d.Name));
items.AddRange(files.OrderBy(f => f.Name));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error reading directory {path}: {ex.Message}");
if (items.Count == 0 && Directory.GetParent(path) != null)
{
items.Add(new FileSystemItem
{
Name = "..",
FullName = Directory.GetParent(path)!.FullName,
IsDirectory = true
});
}
}
return items;
}
}