diff --git a/src/ZonyLrcTools.Common/MusicInfo.cs b/src/ZonyLrcTools.Common/MusicInfo.cs
index 1c3da9d..0b79324 100644
--- a/src/ZonyLrcTools.Common/MusicInfo.cs
+++ b/src/ZonyLrcTools.Common/MusicInfo.cs
@@ -1,9 +1,11 @@
+using System.Text.RegularExpressions;
+
namespace ZonyLrcTools.Common
{
///
/// 歌曲信息的承载类,携带歌曲的相关数据。
///
- public class MusicInfo
+ public partial class MusicInfo
{
///
/// 歌曲对应的物理文件路径。
@@ -38,9 +40,17 @@ namespace ZonyLrcTools.Common
/// 歌曲的作者。
public MusicInfo(string filePath, string name, string artist)
{
- FilePath = filePath;
+ FilePath = Path.Combine(Path.GetDirectoryName(filePath)!, HandleInvalidFilePath(Path.GetFileName(filePath)));
Name = name;
Artist = artist;
}
+
+ private string HandleInvalidFilePath(string srcText)
+ {
+ return InvalidFilePathRegex().Replace(srcText, "");
+ }
+
+ [GeneratedRegex(@"[<>:""/\\|?*]")]
+ private static partial Regex InvalidFilePathRegex();
}
}
\ No newline at end of file
diff --git a/tests/ZonyLrcTools.Tests/MusicInfoTests.cs b/tests/ZonyLrcTools.Tests/MusicInfoTests.cs
new file mode 100644
index 0000000..282c151
--- /dev/null
+++ b/tests/ZonyLrcTools.Tests/MusicInfoTests.cs
@@ -0,0 +1,15 @@
+using Shouldly;
+using Xunit;
+using ZonyLrcTools.Common;
+
+namespace ZonyLrcTools.Tests;
+
+public class MusicInfoTests
+{
+ [Fact]
+ public void InvalidFilePathTest()
+ {
+ var musicInfo = new MusicInfo("C:\\Users\\Zony\\Music\\[ZonyLrcTools]:? - 01. 你好.mp3", "你好", "Zony");
+ musicInfo.FilePath.ShouldBe(@"C:\Users\Zony\Music\[ZonyLrcTools] - 01. 你好.mp3");
+ }
+}
\ No newline at end of file