mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 15:30:27 +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))
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user