/// <summary> /// Determine what kind of filesystem to use. /// After this point we *should theoretically* not need to use System.IO.Path. /// </summary> public static (IFileSystem, DirectoryEntry) DetermineFileSystem(string path, bool readOnly = false) { var emptyPath = string.IsNullOrWhiteSpace(path); var extension = Path.GetExtension(path); Log.Debug($"Determining file system for {path}"); IFileSystem fileSystem; DirectoryEntry baseEntry; if (emptyPath) { fileSystem = new PhysicalFileSystem(); baseEntry = fileSystem.GetDirectoryEntry( fileSystem.ConvertPathFromInternal(Directory.GetCurrentDirectory())); } else { // resolve path (relative to absolute) path = Path.GetFullPath(path); if (Directory.Exists(path) || extension == string.Empty) { var physicalFileSystem = new PhysicalFileSystem(); var internalPath = physicalFileSystem.ConvertPathFromInternal(path); physicalFileSystem.CreateDirectory(internalPath); fileSystem = new SubFileSystem(physicalFileSystem, internalPath); baseEntry = fileSystem.GetDirectoryEntry(UPath.Root); } else { // ensure parent directory exists on disk Directory.CreateDirectory(Path.GetDirectoryName(path)); switch (extension) { case "." + SqlitePattern: throw new PlatformNotSupportedException("See https://github.com/QutEcoacoustics/audio-analysis/issues/289"); /* * fileSystem = new SqliteFileSystem( * path, * readOnly ? OpenMode.ReadOnly : OpenMode.ReadWriteCreate); */ break; default: throw new NotSupportedException( $"Cannot determine file system for given extension {extension}"); } baseEntry = new DirectoryEntry(fileSystem, UPath.Root); } } Log.Debug($"Filesystem for {path} is {fileSystem.GetType().Name}"); return(fileSystem, baseEntry); }
public void 修改資料夾時間() { using var fileSystem = new PhysicalFileSystem(); var executingAssembly = Assembly.GetExecutingAssembly(); var rootPath = Path.GetDirectoryName(executingAssembly.Location); var rootUPath = fileSystem.ConvertPathFromInternal(rootPath); var subName = "TestFolder"; var subPath = $"{rootUPath}/{subName}"; var subPath1 = $"{subPath}/1"; var subFile1 = $"{subPath}/1/1.txt"; var subPath1_1 = $"{subPath}/1/1_1"; var subFile1_1 = $"{subPath}/1/1_1/1_1.txt"; var subPath1_1_1 = (UPath)$"{subPath}/1/1_1/1_1_1"; var subPath2 = $"{subPath}/2"; var content = "This is test string"; var contentBytes = Encoding.UTF8.GetBytes(content); if (fileSystem.DirectoryExists(subPath1_1_1) == false) { fileSystem.CreateDirectory(subPath1_1_1); } var directoryEntry = fileSystem.GetDirectoryEntry(subPath1_1_1); directoryEntry.CreationTime = new DateTime(2000, 1, 1); if (fileSystem.DirectoryExists(subPath2) == false) { fileSystem.CreateDirectory(subPath2); } if (fileSystem.FileExists(subFile1) == false) { using var stream = fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read); stream.Write(contentBytes, 0, contentBytes.Length); } if (fileSystem.FileExists(subFile1_1) == false) { using var stream = fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read); stream.Write(contentBytes, 0, contentBytes.Length); } Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1)); Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1)); Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1)); Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2)); fileSystem.DeleteDirectory(subPath, true); }