The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileHelper.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Microsoft.Win32;
  2. using MimeDetective;
  3. using MimeDetective.Extensions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace Teknik.Utilities
  11. {
  12. public static class FileHelper
  13. {
  14. public static string GenerateRandomFileName(string path, string extension, int length)
  15. {
  16. if (Directory.Exists(path))
  17. {
  18. string filename = StringHelper.RandomString(length);
  19. string subDir = filename[0].ToString();
  20. path = Path.Combine(path, subDir);
  21. if (!Directory.Exists(path))
  22. {
  23. Directory.CreateDirectory(path);
  24. }
  25. while (File.Exists(Path.Combine(path, string.Format("{0}.{1}", filename, extension))))
  26. {
  27. filename = StringHelper.RandomString(length);
  28. subDir = filename[0].ToString();
  29. path = Path.Combine(path, subDir);
  30. if (!Directory.Exists(path))
  31. {
  32. Directory.CreateDirectory(path);
  33. }
  34. }
  35. return Path.Combine(path, string.Format("{0}.{1}", filename, extension));
  36. }
  37. return string.Empty;
  38. }
  39. /// <summary>
  40. /// Append a number to the end of a file name to make it unique, incrementing the number if needed
  41. /// </summary>
  42. /// <param name="file">the name of the file, including extension</param>
  43. /// <param name="destinationFolder">the destination folder in which to check for uniqueness</param>
  44. /// <returns>the file name, without folder, with extension, with a unique index as the suffix, if necessary</returns>
  45. public static string MakeUniqueFilename(string file, string destinationFolder)
  46. {
  47. string fileName = string.Empty;
  48. try
  49. {
  50. string fileBase = Path.GetFileNameWithoutExtension(file);
  51. string fileExt = Path.GetExtension(file);
  52. fileName = string.Format("{0}{1}", fileBase, fileExt);
  53. string filePath = Path.Combine(destinationFolder, fileName);
  54. int iteration = 1;
  55. while (File.Exists(filePath))
  56. {
  57. fileName = string.Format("{0} ({1}){2}", fileBase, iteration, fileExt);
  58. filePath = Path.Combine(destinationFolder, fileName);
  59. iteration++;
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. // Throw up exception to parent
  65. throw new Exception(string.Format("Exception occured in FileHelper.MakeUniqueFilename({0}, {1})", file, destinationFolder), ex);
  66. }
  67. return fileName;
  68. }
  69. public static string GetDefaultExtension(string mimeType)
  70. {
  71. return GetDefaultExtension(mimeType, string.Empty);
  72. }
  73. public static string GetDefaultExtension(string mimeType, string defaultExtension)
  74. {
  75. string result;
  76. RegistryKey key;
  77. object value;
  78. key = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + mimeType, false);
  79. value = key != null ? key.GetValue("Extension", null) : null;
  80. result = value != null ? value.ToString() : defaultExtension;
  81. return result;
  82. }
  83. public static string GetDefaultExtension(Stream fileStream, string defaultExtension)
  84. {
  85. FileType fileType = fileStream.GetFileType();
  86. if (!string.IsNullOrEmpty(fileType.Extension))
  87. return fileType.Extension;
  88. return defaultExtension;
  89. }
  90. }
  91. }