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 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(); } var items = new List(); 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; } }