From 20d5193bd843dabc272eb963fb47130d1ded2d8f Mon Sep 17 00:00:00 2001 From: lc6464 <64722907+lc6464@users.noreply.github.com> Date: Mon, 4 Mar 2024 22:32:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=B9=E9=87=8F=E7=BC=96?= =?UTF-8?q?=E8=A7=A3=E7=A0=81=E6=95=B4=E4=B8=AA=E6=96=87=E4=BB=B6=E5=A4=B9?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E4=BC=9A=E8=87=AA=E5=8A=A8=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=AD=90=E6=96=87=E4=BB=B6=E5=A4=B9=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=9B=E6=B7=BB=E5=8A=A0=E5=AF=B9=20DEBUG=20=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E7=9A=84=E5=BF=AB=E9=80=9F=E6=93=8D=E4=BD=9C=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Base16384.Net.csproj | 5 +++-- Helpers.cs | 1 - Program.cs | 45 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Base16384.Net.csproj b/Base16384.Net.csproj index 7f4a039..d8f8d2d 100644 --- a/Base16384.Net.csproj +++ b/Base16384.Net.csproj @@ -11,8 +11,9 @@ - - + + + \ No newline at end of file diff --git a/Helpers.cs b/Helpers.cs index 96be52d..86557a7 100644 --- a/Helpers.cs +++ b/Helpers.cs @@ -1,5 +1,4 @@ using System.Diagnostics.CodeAnalysis; -using System.Globalization; namespace ConsoleHelpers; diff --git a/Program.cs b/Program.cs index eaae04a..610ffb2 100644 --- a/Program.cs +++ b/Program.cs @@ -8,6 +8,23 @@ 4. 编解码失败 5. 无法创建输出文件夹(可能是输出文件夹是一个已存在的文件) */ +if (args is ["debug", string]) { + var lower = args[1].ToLower(new CultureInfo("en-US", false)); + if (lower == "enable") { + Environment.SetEnvironmentVariable("Base16384_Net_Debug", "true"); + Console.WriteLine("Debug mode is enabled."); + return 0; + } + if (lower == "disable") { + Environment.SetEnvironmentVariable("Base16384_Net_Debug", null); + Console.WriteLine("Debug mode is disabled."); + return 0; + } + + Console.WriteLine("Usage: Base16384.Net.exe <\"e\" | \"d\"> [out | \"-\"]"); + return 1; +} + if (args.Length is not 2 and not 3 || args is not ["e", ..] and not ["d", ..]) { Console.WriteLine("Usage: Base16384.Net.exe <\"e\" | \"d\"> [out | \"-\"]"); @@ -93,12 +110,12 @@ 5. 无法创建输出文件夹(可能是输出文件夹是一个已存在的 output = new(Path.Combine(outputDirectoryInfo.FullName, input.Name)); if (output.Exists) { - Console.WriteLine($"{input.Name} -> {output.Name} ... The output file in the directory {outputDirectoryInfo.Name} already exists."); + Console.WriteLine($"{input.Name} -> {output.Name} ... The output file in the directory \"{outputDirectoryInfo.Name}\" already exists."); continue; } if (!input.Exists) { - Console.WriteLine($"{input.Name} -> Source file not found."); + Console.WriteLine($"{input.Name} -> {output.Name} ... Source file not found."); continue; } @@ -131,20 +148,38 @@ 5. 无法创建输出文件夹(可能是输出文件夹是一个已存在的 } FileInfo output; - string relativePath; + DirectoryInfo outputParentDirectoryInfo; + string relativePath, relativeDirectory; // foreach files in the list foreach (var input in inputDirectoryInfo.EnumerateFiles("*", SearchOption.AllDirectories)) { relativePath = Path.GetRelativePath(inputDirectoryInfo.FullName, input.FullName); output = new(Path.Combine(outputDirectoryInfo.FullName, relativePath)); + relativeDirectory = Path.GetDirectoryName(relativePath)!; + relativeDirectory = string.IsNullOrWhiteSpace(relativeDirectory) ? "." : relativeDirectory; + + outputParentDirectoryInfo = output.Directory!; + + if (!outputParentDirectoryInfo.Exists) { + if (File.Exists(outputParentDirectoryInfo.FullName)) { + Console.WriteLine($"{input.Name} -> {output.Name} ... The output directory \"{relativeDirectory}\" is an existing file."); + } + + try { + outputParentDirectoryInfo.Create(); + } catch (Exception e) { + Helpers.PrintException($"{input.Name} -> {output.Name} ... Failed to create output directory \"{outputParentDirectoryInfo.FullName}\".", e, 5); + continue; + } + } if (output.Exists) { - Console.WriteLine($"{input.Name} -> {output.Name} ... The output file in the directory {Path.GetDirectoryName(relativePath)} already exists."); + Console.WriteLine($"{input.Name} -> {output.Name} ... The output file in the directory \"{relativeDirectory}\" already exists."); continue; } if (!input.Exists) { - Console.WriteLine($"{input.Name} -> Source file not found."); + Console.WriteLine($"{input.Name} -> {output.Name} ... Source file not found."); continue; }