failu_sistema_iesk using System; using System.Collections.Generic; using System.IO; using System.Linq; // Enum tips: Folder (mape), File (datne) enum FileType { Folder, File } // Klasē tiek definēta failu sistēmas loģika class FileSystemObject { public string Name { get; set; } public FileSystemObject Parent { get; set; } public List Children { get; set; } = new List(); public FileType Type { get; set; } public FileSystemObject(string name, FileType type, FileSystemObject parent = null) { Name = name; Type = type; Parent = parent; } public FileSystemObject Navigate(string folderName) { return Children.FirstOrDefault(c => c.Name.Equals(folderName, StringComparison.OrdinalIgnoreCase) && c.Type == FileType.Folder); } public void ShowChildren() { foreach (var child in Children) { if (child.Type == FileType.Folder) Console.WriteLine("/[" + child.Name + "]"); else Console.WriteLine("/~[" + child.Name + "]~"); } } public void DeleteFolder(string name) { var folder = Children.FirstOrDefault(c => c.Name == name && c.Type == FileType.Folder); if (folder != null) { if (folder.Children.Any()) { Console.Write($"Mape '{name}' satur objektus. Vai tiešām dzēst? (y/n): "); string confirm = Console.ReadLine().ToLower(); if (confirm != "y") return; } Children.Remove(folder); Console.WriteLine($"Mape '{name}' izdzēsta!"); } else { Console.WriteLine("Mape nav atrasta!"); } } public void DeleteFile(string name) { var file = Children.FirstOrDefault(c => c.Name == name && c.Type == FileType.File); if (file != null) { string path = $"{name}.txt"; if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } Children.Remove(file); Console.WriteLine($"Datne '{name}' izdzēsta!"); } else { Console.WriteLine("Datne nav atrasta!"); } } public void CreateFolders(string[] names) { foreach (var name in names) { if (Children.Any(c => c.Name == name)) { Console.WriteLine($"'{name}' jau eksistē!"); continue; } Children.Add(new FileSystemObject(name, FileType.Folder, this)); } } public void CreateFiles(string[] names) { foreach (var name in names) { if (Children.Any(c => c.Name == name)) { Console.WriteLine($"'{name}' jau eksistē!"); continue; } string path = $"{name}.txt"; System.IO.File.WriteAllText(path, ""); Children.Add(new FileSystemObject(name, FileType.File, this)); } } public void DirToFile(string filename) { string path = $"{filename}.txt"; List lines = new List(); foreach (var child in Children) { if (child.Type == FileType.Folder) lines.Add("/[" + child.Name + "]"); else lines.Add("/~[" + child.Name + "]~"); } System.IO.File.WriteAllLines(path, lines); var existing = Children.FirstOrDefault(c => c.Name == filename && c.Type == FileType.File); if (existing != null) Children.Remove(existing); Children.Add(new FileSystemObject(filename, FileType.File, this)); Console.WriteLine($"Saturs izvadīts uz '{filename}.txt'"); } public void EditFile(string filename) { var file = Children.FirstOrDefault(c => c.Name == filename && c.Type == FileType.File); if (file != null) { string path = $"{filename}.txt"; if (System.IO.File.Exists(path)) { Console.WriteLine($"== {filename}.txt =="); Console.WriteLine(System.IO.File.ReadAllText(path)); } else { Console.WriteLine("Datnes fails nav atrasts!"); } } else { Console.WriteLine("Datne nav atrasta!"); } } public string GetFullPath() { if (Parent == null) return "c:"; return Parent.GetFullPath() + "/" + Name; } } // Programmas ieejas punkts (galvenā metode) class Program { static void Main() { FileSystemObject root = new FileSystemObject("c:", FileType.Folder); FileSystemObject current = root; Console.WriteLine("Virtuālā failu sistēma sākta. Ieraksti 'exit' vai 'by', lai beigtu."); while (true) { try { Console.Write($"{current.GetFullPath()}/> "); string input = Console.ReadLine().Trim(); if (string.IsNullOrEmpty(input)) continue; string[] parts = input.Split(' ', 2); string command = parts[0].ToLower(); string[] args = parts.Length > 1 ? parts[1].Split(' ') : Array.Empty(); switch (command) { case "exit": case "by": return; case "mkdir": if (args.Length == 0 || args.Length > 3) Console.WriteLine("Maks. 3 mapes!"); else current.CreateFolders(args); break; case "create": if (args.Length == 0 || args.Length > 3) Console.WriteLine("Maks. 3 datnes!"); else current.CreateFiles(args); break; case "rm": foreach (var name in args) current.DeleteFolder(name); break; case "del": foreach (var name in args) current.DeleteFile(name); break; case "cd": if (args.Length == 0) break; if (args[0] == "..") current = current.Parent ?? current; else if (args[0] == "/") current = root; else { var next = current.Navigate(args[0]); if (next != null) current = next; else Console.WriteLine("Mape nav atrasta."); } break; case "dir": if (args.Length == 1 && args[0].StartsWith(">")) { current.DirToFile(args[0].Substring(1)); } else { current.ShowChildren(); } break; case "edit": if (args.Length == 1) current.EditFile(args[0]); else Console.WriteLine("Norādi vienu datnes nosaukumu!"); break; default: Console.WriteLine("Komanda nav atrasta!"); break; } } catch (Exception ex) { Console.WriteLine($"Kļūda: {ex.Message}"); } } } }