From 572e555f2ae839cd97bc7bb1d8103f2c853e398f Mon Sep 17 00:00:00 2001 From: real-zony Date: Sun, 19 Mar 2023 11:28:04 +0800 Subject: [PATCH] fix: Fixed the path issue #128. We should avoid using the Directory.GetCurrentDirectory() method, as it can result in retrieving incorrect paths. --- src/ZonyLrcTools.Cli/Program.cs | 2 +- .../DependencyInject/ServiceCollectionExtensions.cs | 2 +- .../Infrastructure/Exceptions/ErrorCodeHelper.cs | 2 +- src/ZonyLrcTools.Common/Lyrics/LyricsDownloader.cs | 2 +- .../Infrastructure/Album/QQMusicAlbumDownloaderTests.cs | 5 +++-- .../ZonyLrcTools.Tests/Infrastructure/Tag/TagLoaderTests.cs | 4 ++-- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/ZonyLrcTools.Cli/Program.cs b/src/ZonyLrcTools.Cli/Program.cs index 972e029..9b26dde 100644 --- a/src/ZonyLrcTools.Cli/Program.cs +++ b/src/ZonyLrcTools.Cli/Program.cs @@ -83,7 +83,7 @@ namespace ZonyLrcTools.Cli .ConfigureHostConfiguration(builder => { builder - .SetBasePath(Directory.GetCurrentDirectory()) + .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddYamlFile("config.yaml"); }) .ConfigureServices((_, services) => diff --git a/src/ZonyLrcTools.Common/Infrastructure/DependencyInject/ServiceCollectionExtensions.cs b/src/ZonyLrcTools.Common/Infrastructure/DependencyInject/ServiceCollectionExtensions.cs index 4a48281..27f9054 100644 --- a/src/ZonyLrcTools.Common/Infrastructure/DependencyInject/ServiceCollectionExtensions.cs +++ b/src/ZonyLrcTools.Common/Infrastructure/DependencyInject/ServiceCollectionExtensions.cs @@ -43,7 +43,7 @@ namespace ZonyLrcTools.Common.Infrastructure.DependencyInject public static IServiceCollection ConfigureConfiguration(this IServiceCollection services) { var configuration = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) + .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddYamlFile("config.yaml") .Build(); diff --git a/src/ZonyLrcTools.Common/Infrastructure/Exceptions/ErrorCodeHelper.cs b/src/ZonyLrcTools.Common/Infrastructure/Exceptions/ErrorCodeHelper.cs index 2c877e0..cf4ac69 100644 --- a/src/ZonyLrcTools.Common/Infrastructure/Exceptions/ErrorCodeHelper.cs +++ b/src/ZonyLrcTools.Common/Infrastructure/Exceptions/ErrorCodeHelper.cs @@ -26,7 +26,7 @@ namespace ZonyLrcTools.Common.Infrastructure.Exceptions return; } - var jsonPath = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "error_msg.json"); + var jsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "error_msg.json"); using var jsonReader = new JsonTextReader(File.OpenText(jsonPath)); var jsonObj = JObject.Load(jsonReader); diff --git a/src/ZonyLrcTools.Common/Lyrics/LyricsDownloader.cs b/src/ZonyLrcTools.Common/Lyrics/LyricsDownloader.cs index 032edd5..f3fbbf1 100644 --- a/src/ZonyLrcTools.Common/Lyrics/LyricsDownloader.cs +++ b/src/ZonyLrcTools.Common/Lyrics/LyricsDownloader.cs @@ -67,7 +67,7 @@ public class LyricsDownloader : ILyricsDownloader, ISingletonDependency await Task.WhenAll(downloadTasks); await _logger.InfoAsync($"歌词数据下载完成,成功: {needDownloadMusicInfos.Count(m => m.IsSuccessful)} 失败{needDownloadMusicInfos.Count(m => m.IsSuccessful == false)}。"); - await LogFailedSongFilesInfo(Path.Combine(Directory.GetCurrentDirectory(), $"歌词下载失败列表_{DateTime.Now:yyyyMMddHHmmss}.txt"), needDownloadMusicInfos); + await LogFailedSongFilesInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"歌词下载失败列表_{DateTime.Now:yyyyMMddHHmmss}.txt"), needDownloadMusicInfos); } private async Task DownloadAndWriteLyricsAsync(ILyricsProvider provider, MusicInfo info) diff --git a/tests/ZonyLrcTools.Tests/Infrastructure/Album/QQMusicAlbumDownloaderTests.cs b/tests/ZonyLrcTools.Tests/Infrastructure/Album/QQMusicAlbumDownloaderTests.cs index 37646d7..ce8d551 100644 --- a/tests/ZonyLrcTools.Tests/Infrastructure/Album/QQMusicAlbumDownloaderTests.cs +++ b/tests/ZonyLrcTools.Tests/Infrastructure/Album/QQMusicAlbumDownloaderTests.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -20,7 +21,7 @@ namespace ZonyLrcTools.Tests.Infrastructure.Album albumBytes.Length.ShouldBeGreaterThan(0); // 显示具体的图像。 - var tempAlbumPath = Path.Combine(Directory.GetCurrentDirectory(), "tempAlbum.png"); + var tempAlbumPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tempAlbum.png"); File.Delete(tempAlbumPath); await using var file = File.Create(tempAlbumPath); diff --git a/tests/ZonyLrcTools.Tests/Infrastructure/Tag/TagLoaderTests.cs b/tests/ZonyLrcTools.Tests/Infrastructure/Tag/TagLoaderTests.cs index 8ce6174..3f7df02 100644 --- a/tests/ZonyLrcTools.Tests/Infrastructure/Tag/TagLoaderTests.cs +++ b/tests/ZonyLrcTools.Tests/Infrastructure/Tag/TagLoaderTests.cs @@ -1,9 +1,9 @@ +using System; using System.IO; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Shouldly; using Xunit; -using ZonyLrcTools.Cli.Infrastructure.Tag; using ZonyLrcTools.Common.TagInfo; namespace ZonyLrcTools.Tests.Infrastructure.Tag @@ -16,7 +16,7 @@ namespace ZonyLrcTools.Tests.Infrastructure.Tag var tagLoader = ServiceProvider.GetRequiredService(); tagLoader.ShouldNotBeNull(); - var info = await tagLoader.LoadTagAsync(Path.Combine(Directory.GetCurrentDirectory(), "MusicFiles", "曾经艺也 - 荀彧(纯音乐版).mp3")); + var info = await tagLoader.LoadTagAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MusicFiles", "曾经艺也 - 荀彧(纯音乐版).mp3")); info.ShouldNotBeNull(); info.Name.ShouldBe("荀彧(纯音乐版)"); info.Artist.ShouldBe("曾经艺也");