SmbFileExtensions.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SmbFileExtensions.cs implementation by J. Arturo <webmaster at komodosoft dot net>
  2. //
  3. // This library is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU Lesser General Public
  5. // License as published by the Free Software Foundation; either
  6. // version 2.1 of the License, or (at your option) any later version.
  7. //
  8. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // Lesser General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU Lesser General Public
  14. // License along with this library; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. using System;
  17. using System.Threading.Tasks;
  18. using SharpCifs.Util.Sharpen;
  19. namespace SharpCifs.Smb
  20. {
  21. public static class SmbFileExtensions
  22. {
  23. /// <summary>
  24. /// Get file's creation date converted to local timezone
  25. /// </summary>
  26. /// <param name="smbFile"></param>
  27. /// <returns></returns>
  28. public static DateTime GetLocalCreateTime(this SmbFile smbFile)
  29. {
  30. return TimeZoneInfo.ConvertTime(Extensions.CreateDateFromUTC(smbFile.CreateTime()),
  31. TimeZoneInfo.Local);
  32. }
  33. /// <summary>
  34. /// Get file's last modified date converted to local timezone
  35. /// </summary>
  36. /// <param name="smbFile"></param>
  37. /// <returns></returns>
  38. public static DateTime GetLocalLastModified(this SmbFile smbFile)
  39. {
  40. return TimeZoneInfo.ConvertTime(Extensions.CreateDateFromUTC(smbFile.LastModified()),
  41. TimeZoneInfo.Local);
  42. }
  43. /// <summary>
  44. /// List files async
  45. /// </summary>
  46. /// <param name="smbFile"></param>
  47. /// <returns></returns>
  48. public static Task<SmbFile[]> ListFilesAsync(this SmbFile smbFile)
  49. {
  50. return Task.Run(() => smbFile.ListFiles());
  51. }
  52. /// <summary>
  53. /// List files async
  54. /// </summary>
  55. /// <param name="smbFile"></param>
  56. /// <param name="wildcard"></param>
  57. /// <returns></returns>
  58. public static Task<SmbFile[]> ListFilesAsync(this SmbFile smbFile, string wildcard)
  59. {
  60. return Task.Run(() => smbFile.ListFiles(wildcard));
  61. }
  62. /// <summary>
  63. /// List files async
  64. /// </summary>
  65. /// <param name="smbFile"></param>
  66. /// <returns></returns>
  67. public static Task<string[]> ListAsync(this SmbFile smbFile)
  68. {
  69. return Task.Run(() => smbFile.List());
  70. }
  71. /// <summary>
  72. /// MkDir async method
  73. /// </summary>
  74. /// <param name="smbFile"></param>
  75. /// <returns></returns>
  76. public static Task MkDirAsync(this SmbFile smbFile)
  77. {
  78. return Task.Run(() => smbFile.Mkdir());
  79. }
  80. /// <summary>
  81. /// Delete file async
  82. /// </summary>
  83. /// <param name="smbFile"></param>
  84. /// <returns></returns>
  85. public static Task DeleteAsync(this SmbFile smbFile)
  86. {
  87. return Task.Run(() => smbFile.Delete());
  88. }
  89. /// <summary>
  90. /// Rename file async
  91. /// </summary>
  92. /// <param name="smbFile"></param>
  93. /// <param name="destination"></param>
  94. /// <returns></returns>
  95. public static Task RenameToAsync(this SmbFile smbFile, SmbFile destination)
  96. {
  97. return Task.Run(() => smbFile.RenameTo(destination));
  98. }
  99. /// <summary>
  100. /// Get input stream async
  101. /// </summary>
  102. /// <param name="smbFile"></param>
  103. /// <returns></returns>
  104. public static Task<InputStream> GetInputStreamAsync(this SmbFile smbFile)
  105. {
  106. return Task.Run(() => smbFile.GetInputStream());
  107. }
  108. /// <summary>
  109. /// Get output stream async
  110. /// </summary>
  111. /// <param name="smbFile"></param>
  112. /// <param name="append"></param>
  113. /// <returns></returns>
  114. public static Task<OutputStream> GetOutputStreamAsync(this SmbFile smbFile, bool append = false)
  115. {
  116. return Task.Run(() => new SmbFileOutputStream(smbFile, append) as OutputStream);
  117. }
  118. }
  119. }