重构虚拟文件系统

master
zc 2024-12-19 23:07:52 +08:00
parent 1692f02643
commit e28547e01f
4 changed files with 152 additions and 38 deletions

View File

@ -4,17 +4,19 @@ namespace ZXLT.FileSystem
{
public sealed class DirectoryInfo : FileSystemInfo
{
List<DirectoryInfo> directories = new List<DirectoryInfo>();
List<FileInfo> files = new List<FileInfo>();
// List<DirectoryInfo> directories = new List<DirectoryInfo>();
// List<FileInfo> files = new List<FileInfo>();
public override bool Exists { get; }
public override string Name => _name;
public override FileSystemInfoType FileSystemInfoType { get; }
public DirectoryInfo Parent
{
get
{
var directoryInfo = new DirectoryInfo(System.IO.Path.GetDirectoryName(FullPath));
var parentPath = FullPath.Split("\\")[^1];
var directoryInfo = new DirectoryInfo(System.IO.Path.GetFullPath(parentPath));
return directoryInfo;
}
}
@ -23,28 +25,18 @@ namespace ZXLT.FileSystem
public DirectoryInfo(string path)
{
this.Init(path, System.IO.Path.GetFullPath(path), isNormalized: true);
FullPath = path;
_name = path;
FullPath = System.IO.Path.GetFullPath(path);
_name = System.IO.Path.GetDirectoryName(path);
}
private void Init(string originalPath, string fullPath = null, string fileName = null, bool isNormalized = false)
{
if (fullPath == null)
fullPath = originalPath;
fullPath = isNormalized ? fullPath : System.IO.Path.GetFullPath(fullPath);
this._name = fileName;
this.FullPath = fullPath;
}
public DirectoryInfo[] GetDirectories()
{
return directories.ToArray();
}
public FileInfo[] GetFiles()
{
return files.ToArray();
}
// public DirectoryInfo[] GetDirectories()
// {
// return directories.ToArray();
// }
//
// public FileInfo[] GetFiles()
// {
// return files.ToArray();
// }
}
}

View File

@ -5,9 +5,10 @@ namespace ZXLT.FileSystem
{
public sealed class FileInfo : FileSystemInfo
{
public string Extension { get; }
public string Extension => Path.GetExtension(this.FullName);
public override bool Exists { get; }
public override string Name => _name;
public override FileSystemInfoType FileSystemInfoType { get; }
public string? DirectoryName => Path.GetDirectoryName(this.FullPath);
public DirectoryInfo Directory
@ -20,16 +21,11 @@ namespace ZXLT.FileSystem
}
public FileInfo(string fileName)
: this(fileName ?? throw new ArgumentNullException(nameof(fileName)), (string)null, (string)null, false)
{
}
internal FileInfo(string originalPath, string fullPath = null, string fileName = null, bool isNormalized = false)
{
if (fullPath == null)
fullPath = originalPath;
this.FullPath = isNormalized ? fullPath ?? originalPath : Path.GetFullPath(fullPath);
this._name = fileName;
fileName = fileName ?? throw new ArgumentNullException(nameof(fileName));
_name = Path.GetFileName(fileName);
this.FullPath = Path.GetFullPath(fileName);
FileSystemInfoType = FileSystemInfoType.File;
}
}
}

View File

@ -1,7 +1,126 @@
namespace ZXLT.FileSystem
using System;
using System.Collections.Generic;
using ZXL.Helper;
namespace ZXLT.FileSystem
{
public class FileSystem
class FileBase
{
}
class Directory : FileBase
{
DirectoryInfo directoryInfo;
List<Directory> directories = new List<Directory>();
List<File> files = new List<File>();
public Directory(DirectoryInfo directoryInfo)
{
this.directoryInfo = directoryInfo;
}
public void AddDirectory(Directory dir)
{
directories.Add(dir);
}
public void AddFile(File file)
{
files.Add(file);
}
public void RemoveDirectory(Directory dir)
{
directories.Remove(dir);
}
public void RemoveFile(File file)
{
files.Remove(file);
}
public void Clear()
{
directories.Clear();
}
}
class File : FileBase
{
FileInfo fileInfo;
public File(FileInfo fileInfo)
{
this.fileInfo = fileInfo;
}
}
interface IFileSystem
{
event Action<FileSystemInfo> OnUpdateFileSystem;
void CreateDirectory(string path);
void DeleteDirectory(string path);
void DeleteFile(string path);
void MoveDirectory(string sourcePath, string destPath);
void MoveFile(string sourcePath, string destPath);
void RenameDirectory(string sourcePath, string destPath);
void RenameFile(string sourcePath, string destPath);
}
public class FileSystem : IFileSystem
{
Directory rootDirectory;
SortedDictionary<string, FileBase> fileBases = new SortedDictionary<string, FileBase>();
public event Action<FileSystemInfo> OnUpdateFileSystem;
void UpdateFileSystem(FileSystemInfo systemInfo)
{
OnUpdateFileSystem?.Invoke(systemInfo);
}
public void CreateDirectory(string path)
{
var directory = new Directory(new DirectoryInfo(path));
fileBases.Add(path, directory);
FileHelper.CreateDirectory(path);
}
public void CreateFile(string path)
{
var file = new File(new FileInfo(path));
fileBases.Add(path, file);
FileHelper.CreateDirectory(path);
}
public void DeleteDirectory(string path)
{
throw new NotImplementedException();
}
public void DeleteFile(string path)
{
throw new NotImplementedException();
}
public void MoveDirectory(string sourcePath, string destPath)
{
throw new NotImplementedException();
}
public void MoveFile(string sourcePath, string destPath)
{
throw new NotImplementedException();
}
public void RenameDirectory(string sourcePath, string destPath)
{
throw new NotImplementedException();
}
public void RenameFile(string sourcePath, string destPath)
{
throw new NotImplementedException();
}
}
}

View File

@ -2,13 +2,20 @@
namespace ZXLT.FileSystem
{
public enum FileSystemInfoType
{
File,
Directory,
}
public abstract class FileSystemInfo
{
protected string _name;
protected string FullPath;
public DateTime CreationTime { get; set; }
public DateTime CreationTime { get; }
public abstract bool Exists { get; }
public abstract string Name { get; }
public virtual string FullName => FullPath;
public abstract FileSystemInfoType FileSystemInfoType { get; }
}
}