From 5b69dffad16517caf36b862388f73186727c1732 Mon Sep 17 00:00:00 2001 From: Damon Lu <59256766+WhatDamon@users.noreply.github.com> Date: Thu, 22 Aug 2024 03:15:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=A7=E5=B0=8F=E8=AE=A1?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WndMain.Designer.cs | 12 ++++++++ WndMain.cs | 74 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/WndMain.Designer.cs b/WndMain.Designer.cs index 476ec1f..7ce8250 100644 --- a/WndMain.Designer.cs +++ b/WndMain.Designer.cs @@ -42,6 +42,7 @@ private void InitializeComponent() linkLabel1 = new LinkLabel(); fileFolderComboBox = new ComboBox(); scanMoreFoldersCheckBox = new CheckBox(); + filesizeLabel = new Label(); statusStrip1.SuspendLayout(); SuspendLayout(); // @@ -60,6 +61,7 @@ private void InitializeComponent() filepathTextBox.Name = "filepathTextBox"; filepathTextBox.Size = new Size(379, 30); filepathTextBox.TabIndex = 1; + filepathTextBox.TextChanged += filepathTextBox_TextChanged; // // browseButton // @@ -161,12 +163,21 @@ private void InitializeComponent() scanMoreFoldersCheckBox.UseVisualStyleBackColor = true; scanMoreFoldersCheckBox.Visible = false; // + // filesizeLabel + // + filesizeLabel.AutoSize = true; + filesizeLabel.Location = new Point(134, 77); + filesizeLabel.Name = "filesizeLabel"; + filesizeLabel.Size = new Size(0, 24); + filesizeLabel.TabIndex = 9; + // // WndMain // AllowDrop = true; AutoScaleDimensions = new SizeF(11F, 24F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(531, 152); + Controls.Add(filesizeLabel); Controls.Add(scanMoreFoldersCheckBox); Controls.Add(fileFolderComboBox); Controls.Add(linkLabel1); @@ -206,5 +217,6 @@ private void InitializeComponent() private LinkLabel linkLabel1; private ComboBox fileFolderComboBox; private CheckBox scanMoreFoldersCheckBox; + private Label filesizeLabel; } } diff --git a/WndMain.cs b/WndMain.cs index 5aa8b23..6107fa9 100644 --- a/WndMain.cs +++ b/WndMain.cs @@ -120,6 +120,65 @@ private void browseButton_Click(object sender, EventArgs e) } } + public class FileFolderSizeRetriever + { + public static string GetSizeAsString(string path, bool includeSubdirectories) + { + long fileSizeBytes = 0; // 初始化变量 + if (File.Exists(path)) + { + FileInfo fileInfo = new FileInfo(path); + fileSizeBytes = fileInfo.Length; + } + else if (Directory.Exists(path)) + { + DirectoryInfo directory = new DirectoryInfo(path); + FileInfo[] files = directory.GetFiles("*.ncm", includeSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); + + foreach (FileInfo file in files) + { + fileSizeBytes += file.Length; + } + } + else + { + return "路径不存在"; + } + + const long oneKb = 1024; + const long oneMb = oneKb * 1024; + + if (fileSizeBytes < oneKb) + { + return $"{fileSizeBytes} 字节"; + } + else if (fileSizeBytes < oneMb) + { + return $"{fileSizeBytes / oneKb} KiB"; + } + else + { + return $"{fileSizeBytes / oneMb} MiB"; + } + } + + private static long GetDirectorySize(DirectoryInfo directory) + { + long totalSize = 0; + FileInfo[] fileInfos = directory.GetFiles(); + foreach (FileInfo fileInfo in fileInfos) + { + totalSize += fileInfo.Length; + } + DirectoryInfo[] subDirectories = directory.GetDirectories(); + foreach (DirectoryInfo subDirectory in subDirectories) + { + totalSize += GetDirectorySize(subDirectory); + } + return totalSize; + } + } + // 检查ncm二进制文件 private bool CheckNCMBinary(string filePath) { @@ -204,6 +263,7 @@ public int ProcessNCMFile(string path) // “转换”按钮被点击 private void convertButton_Click(object sender, EventArgs e) { + filesizeLabel.Text = FileFolderSizeRetriever.GetSizeAsString(filepathTextBox.Text, scanMoreFoldersCheckBox.Checked); if (!File.Exists(GlobalVariables.libncmdumpPath)) { MessageBox.Show("核心不存在\n请确认libncmdump.dll与本程序在同一目录", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -234,7 +294,8 @@ private void convertButton_Click(object sender, EventArgs e) string directoryPath = filepathTextBox.Text; string fileExtension = ".ncm"; string[] files = Directory.GetFiles(directoryPath, "*.*", scanMoreFoldersCheckBox.Checked ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).Where(file => Path.GetExtension(file).ToLower() == fileExtension.ToLower()).ToArray(); - + int fileCount = Directory.GetFiles(directoryPath).Length; + int unprocessFiles = fileCount - files.Length; toolStripProgressBar1.Maximum = files.Length; foreach (var file in files) { @@ -254,11 +315,11 @@ private void convertButton_Click(object sender, EventArgs e) toolStripProgressBar1.Value = allProcessedFiles; toolStripStatusLabel2.Text = "已处理:" + allProcessedFiles.ToString(); } - toolStripStatusLabel2.Text = "成功:" + processedFiles.ToString() + ";失败:" + bypassFiles.ToString(); + toolStripStatusLabel2.Text = "成功:" + processedFiles.ToString() + ";失败:" + bypassFiles.ToString() + ";跳过:" + unprocessFiles.ToString(); } else if (fileFolderComboBox.SelectedIndex == 0) { - toolStripProgressBar1.Maximum = 1; + toolStripProgressBar1.Maximum = 2; if (!filepathTextBox.Text.EndsWith(".ncm")) { MessageBox.Show("这似乎并不是ncm文件!\n请提供ncm文件", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -338,7 +399,7 @@ private void fileFolderComboBox_SelectedIndexChanged(object sender, EventArgs e) { filepathTextBox.Text = ""; } - else if(modifyByDrag) + else if (modifyByDrag) { modifyByDrag = false; } @@ -356,5 +417,10 @@ private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs { MessageBox.Show("注意!\n此应用只用于学习用途,禁止用于商业或违法用途,\n请在遵守NCM文件提供平台的服务条款下使用本应用,\n作者对商业或违法使用本软件造成的任何后果不承担任何责任!", "免责声明", MessageBoxButtons.OK, MessageBoxIcon.Information); } + + private void filepathTextBox_TextChanged(object sender, EventArgs e) + { + filesizeLabel.Text = ""; + } } }