ls命令增强

This commit is contained in:
2026-02-13 14:06:44 +08:00
parent 82ecd84a35
commit f3226eb2ea
3 changed files with 10 additions and 9 deletions

View File

@@ -1 +1 @@
2026-02-12 23:50:58 2026-02-13 13:54:14

View File

@@ -1 +1 @@
6107ecc 82ecd84

View File

@@ -114,50 +114,51 @@ namespace CMLeonOS
{ {
if (Directory.Exists(fullPath)) if (Directory.Exists(fullPath))
{ {
// 列出当前目录下的文件和子目录
string displayPath = path == "." ? CurrentDirectory : path; string displayPath = path == "." ? CurrentDirectory : path;
Console.WriteLine($"Contents of {displayPath}:"); Console.WriteLine($"Contents of {displayPath}:");
// 列出子目录
try try
{ {
var dirs = Directory.GetDirectories(fullPath); var dirs = Directory.GetDirectories(fullPath);
foreach (var dir in dirs) foreach (var dir in dirs)
{ {
// 使用Path.GetFileName获取目录名避免Substring可能导致的问题
string dirName = Path.GetFileName(dir); string dirName = Path.GetFileName(dir);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"[DIR] {dirName}"); Console.WriteLine($"[DIR] {dirName}");
Console.ResetColor();
} }
} }
catch catch
{ {
// 可能没有权限或其他错误
} }
// 列出文件
try try
{ {
var files = Directory.GetFiles(fullPath); var files = Directory.GetFiles(fullPath);
foreach (var file in files) foreach (var file in files)
{ {
// 使用Path.GetFileName获取文件名避免Substring可能导致的问题
string fileName = Path.GetFileName(file); string fileName = Path.GetFileName(file);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"[FILE] {fileName}"); Console.WriteLine($"[FILE] {fileName}");
Console.ResetColor();
} }
} }
catch catch
{ {
// 可能没有权限或其他错误
} }
} }
else else
{ {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Directory not found: {path}"); Console.WriteLine($"Directory not found: {path}");
Console.ResetColor();
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error listing files: {ex.Message}"); Console.WriteLine($"Error listing files: {ex.Message}");
Console.ResetColor();
} }
} }