FileFormatToIconConvert.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Data;
  8. using System.Windows.Media.Imaging;
  9. namespace PDF_Master.DataConvert
  10. {
  11. public class DateTimeToStringConvert : IValueConverter
  12. {
  13. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  14. {
  15. if (value == null)
  16. return null;
  17. else
  18. {
  19. if (value is DateTime)
  20. {
  21. var datetime = (DateTime)value;
  22. return datetime.Year + "-" + datetime.Month +"-"+ datetime.Day + " " + datetime.ToString("HH:mm:ss");
  23. }
  24. return null;
  25. }
  26. }
  27. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  28. {
  29. throw new NotImplementedException();
  30. }
  31. }
  32. public class FilePathToSizeConvert : IValueConverter
  33. {
  34. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  35. {
  36. if (value == null)
  37. return null;
  38. else
  39. {
  40. if (value is string)
  41. {
  42. return GetFileSize((string)value);
  43. }
  44. return null;
  45. }
  46. }
  47. public string GetFileSize(string path)
  48. {
  49. System.IO.FileInfo fileInfo = null;
  50. try
  51. {
  52. fileInfo = new System.IO.FileInfo(path);
  53. }
  54. catch
  55. {
  56. return "0KB";
  57. }
  58. if (fileInfo != null && fileInfo.Exists)
  59. {
  60. var size = Math.Round(fileInfo.Length / 1024.0, 0);
  61. if (size > 1024)
  62. {
  63. var sizeDouble = Math.Round(size / 1024.0,2);
  64. if(sizeDouble > 1024)
  65. {
  66. sizeDouble = Math.Round(sizeDouble / 1024.0,2);
  67. return sizeDouble + "G";
  68. }
  69. else
  70. return sizeDouble + "M";
  71. }
  72. else
  73. {
  74. return (int)System.Math.Ceiling(size) + "KB";
  75. }
  76. }
  77. else
  78. {
  79. return "0KB";
  80. }
  81. }
  82. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  83. {
  84. throw new NotImplementedException();
  85. }
  86. }
  87. }