65 lines
1.8 KiB
C#
65 lines
1.8 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
|
|
namespace RustTools.Editor;
|
|
public class EditorLoad
|
|
{
|
|
/// <summary>
|
|
/// 获取目录下的文件和文件夹
|
|
/// </summary>
|
|
/// <param name="dir"></param>
|
|
/// <returns></returns>
|
|
public static ObservableCollection<FileItem> GetData(string dir)
|
|
{
|
|
var list = new ObservableCollection<FileItem>();
|
|
var directoryInfos = new DirectoryInfo(dir);
|
|
//获取文件夹
|
|
foreach (var file in directoryInfos.GetDirectories())
|
|
{
|
|
var explorerItem = new FileItem(true)
|
|
{
|
|
Name = file.Name,
|
|
Dir = file.FullName,
|
|
};
|
|
//检测是否有子文件
|
|
if (HasChildren(file.FullName))
|
|
{
|
|
explorerItem.Children.Add(new FileItem());
|
|
}
|
|
list.Add(explorerItem);
|
|
}
|
|
//获取文件
|
|
foreach (var file in directoryInfos.GetFiles())
|
|
{
|
|
list.Add(new FileItem()
|
|
{
|
|
Name = file.Name,
|
|
Dir = file.FullName,
|
|
IsFolder = false,
|
|
});
|
|
}
|
|
return list;
|
|
}
|
|
private static bool HasChildren(string path)
|
|
{
|
|
try
|
|
{
|
|
// 检查是否有子目录或子文件
|
|
return Directory.EnumerateDirectories(path).Any() || Directory.EnumerateFiles(path).Any();
|
|
}
|
|
catch (UnauthorizedAccessException)
|
|
{
|
|
// 捕获权限异常(例如无法访问某些系统文件夹)
|
|
return false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine($"Error checking children for path {path}: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|