mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 11:37:01 +00:00
ls命令增强
This commit is contained in:
@@ -1 +1 @@
|
||||
2026-02-12 23:50:58
|
||||
2026-02-13 13:54:14
|
||||
@@ -1 +1 @@
|
||||
6107ecc
|
||||
82ecd84
|
||||
@@ -114,50 +114,51 @@ namespace CMLeonOS
|
||||
{
|
||||
if (Directory.Exists(fullPath))
|
||||
{
|
||||
// 列出当前目录下的文件和子目录
|
||||
string displayPath = path == "." ? CurrentDirectory : path;
|
||||
Console.WriteLine($"Contents of {displayPath}:");
|
||||
|
||||
// 列出子目录
|
||||
try
|
||||
{
|
||||
var dirs = Directory.GetDirectories(fullPath);
|
||||
foreach (var dir in dirs)
|
||||
{
|
||||
// 使用Path.GetFileName获取目录名,避免Substring可能导致的问题
|
||||
string dirName = Path.GetFileName(dir);
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
Console.WriteLine($"[DIR] {dirName}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 可能没有权限或其他错误
|
||||
}
|
||||
|
||||
// 列出文件
|
||||
try
|
||||
{
|
||||
var files = Directory.GetFiles(fullPath);
|
||||
foreach (var file in files)
|
||||
{
|
||||
// 使用Path.GetFileName获取文件名,避免Substring可能导致的问题
|
||||
string fileName = Path.GetFileName(file);
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.WriteLine($"[FILE] {fileName}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 可能没有权限或其他错误
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"Directory not found: {path}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"Error listing files: {ex.Message}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user