123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 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
- {
- /// <summary>
- /// Returns the file size based on the specified file path, with the smallest unit being bytes (B).
- /// </summary>
- /// <param name="filePath">The path to the file.</param>
- /// <returns>
- /// The calculated file size, with units in bytes (B), kilobytes (KB), megabytes (MB), or gigabytes (GB).
- /// </returns>
- 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;
- }
- }
- }
|