From 2f2fb57f6379c8176bc56049febdb5f208c5c7ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20El-Saig?= Date: Mon, 2 Oct 2023 01:01:49 +0200 Subject: [PATCH] Also change directory during Hastlayer.Create(). --- src/Hastlayer/Hast.Common/Services/DirectoryScope.cs | 4 ++-- src/Hastlayer/Hast.Layer/Hastlayer.cs | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Hastlayer/Hast.Common/Services/DirectoryScope.cs b/src/Hastlayer/Hast.Common/Services/DirectoryScope.cs index ff748702..28afd10c 100644 --- a/src/Hastlayer/Hast.Common/Services/DirectoryScope.cs +++ b/src/Hastlayer/Hast.Common/Services/DirectoryScope.cs @@ -15,7 +15,7 @@ public class DirectoryScope : IDisposable public DirectoryScope(string path) { _oldPath = Environment.CurrentDirectory; - Environment.CurrentDirectory = path; + Directory.SetCurrentDirectory(string.IsNullOrWhiteSpace(path) ? "." : path.Trim()); } public void Dispose() @@ -28,7 +28,7 @@ protected virtual void Dispose(bool disposing) { if (!_disposed && Directory.Exists(_oldPath)) { - Environment.CurrentDirectory = _oldPath; + Directory.SetCurrentDirectory(_oldPath); } _disposed = true; diff --git a/src/Hastlayer/Hast.Layer/Hastlayer.cs b/src/Hastlayer/Hast.Layer/Hastlayer.cs index 864a6b65..1883067d 100644 --- a/src/Hastlayer/Hast.Layer/Hastlayer.cs +++ b/src/Hastlayer/Hast.Layer/Hastlayer.cs @@ -119,12 +119,16 @@ public static void ConfigureLogging(IServiceCollection services, Action /// /// Configuration for Hastlayer. + /// + /// If , the executing assembly's directory is used instead of the current working directory. + /// /// A newly created instance. - public static Hastlayer Create(IHastlayerConfiguration configuration) + public static Hastlayer Create(IHastlayerConfiguration configuration, bool inAssemblyDirectory = true) { Argument.ThrowIfNull(configuration, nameof(configuration)); Argument.ThrowIfNull(configuration.Extensions, nameof(configuration.Extensions)); + using var workingDirectory = new DirectoryScope(inAssemblyDirectory ? AppDataFolder.AssemblyDirectory : "."); var hastlayer = new Hastlayer(configuration); hastlayer.LoadHost(); return hastlayer;