Fix subdirectory create failing on root drive (#1432)

This appears to be a bug in .NET. Someone started to fix it in a PR, but it went stale and was auto-closed.

https://github.com/dotnet/runtime/pull/117519
This commit is contained in:
Michael Bucari-Tovo
2025-11-14 13:43:54 -07:00
parent e05dcd6f54
commit 59f66ff480
3 changed files with 27 additions and 3 deletions

View File

@@ -263,5 +263,27 @@ namespace FileManager
return foundFiles;
}
/// <summary>
/// Creates a subdirectory or subdirectories on the specified path.
/// The specified path can be relative to this instance of the <see cref="DirectoryInfo"/> class.
/// <para/>
/// Fixes an issue with <see cref="DirectoryInfo.CreateSubdirectory(string)"/> where it fails when the parent <see cref="DirectoryInfo"/> is a drive root.
/// </summary>
/// <param name="path">The specified path. This cannot be a different disk volume or Universal Naming Convention (UNC) name.</param>
/// <returns>The last directory specified in <paramref name="path"/></returns>
public static DirectoryInfo CreateSubdirectoryEx(this DirectoryInfo parent, string path)
{
if (parent.Root.FullName != parent.FullName || Path.IsPathRooted(path))
return parent.CreateSubdirectory(path);
// parent is a drive root and subDirectory is relative
//Solves a problem with DirectoryInfo.CreateSubdirectory where it fails
//If the parent DirectoryInfo is a drive root.
var fullPath = Path.GetFullPath(Path.Combine(parent.FullName, path));
var directoryInfo = new DirectoryInfo(fullPath);
directoryInfo.Create();
return directoryInfo;
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using FileManager;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
@@ -42,7 +43,7 @@ namespace LibationFileManager
{
// not customizable. don't move to config
private static string ImagesDirectory { get; }
= new DirectoryInfo(Configuration.Instance.LibationFiles).CreateSubdirectory("Images").FullName;
= new DirectoryInfo(Configuration.Instance.LibationFiles).CreateSubdirectoryEx("Images").FullName;
private static string getPath(PictureDefinition def)
=> Path.Combine(ImagesDirectory, $"{def.PictureId}{def.Size}.jpg");

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using DataLayer;
using Dinah.Core;
using FileManager;
using LibationFileManager;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
@@ -279,6 +280,6 @@ namespace LibationSearchEngine
// not customizable. don't move to config
private static string SearchEngineDirectory { get; }
= new System.IO.DirectoryInfo(Configuration.Instance.LibationFiles).CreateSubdirectory("SearchEngine").FullName;
= new System.IO.DirectoryInfo(Configuration.Instance.LibationFiles).CreateSubdirectoryEx("SearchEngine").FullName;
}
}