using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace compdfkit_tools.Helper { public static class CommonHelper { /// /// Returns the file size based on the specified file path, with the smallest unit being bytes (B). /// /// The path to the file. /// /// The calculated file size, with units in bytes (B), kilobytes (KB), megabytes (MB), or gigabytes (GB). /// public static string GetFileSize(string filePath) { FileInfo fileInfo = null; try { fileInfo = new FileInfo(filePath); } catch { return "0B"; } if (fileInfo != null && fileInfo.Exists) { double fileSize = fileInfo.Length; if (fileSize > 1024) { fileSize = Math.Round(fileSize / 1024, 2); if (fileSize > 1024) { fileSize = Math.Round(fileSize / 1024, 2); if (fileSize > 1024) { fileSize = Math.Round(fileSize / 1024, 2); return fileSize + " GB"; } else { return fileSize + " MB"; } } else { return fileSize + " KB"; } } else { return fileSize + " B"; } } return "0B"; } public static string GetFilePathOrEmpty() { string selectedFilePath = string.Empty; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "PDF files (*.pdf)|*.pdf"; if (openFileDialog.ShowDialog() == true) { selectedFilePath = openFileDialog.FileName; } return selectedFilePath; } } }