25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirectoryInfo.Encryption.cs 5.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.IO;
  23. using System.Security;
  24. namespace Alphaleonis.Win32.Filesystem
  25. {
  26. partial class DirectoryInfo
  27. {
  28. #region AlphaFS
  29. #region Decrypt
  30. /// <summary>[AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.</summary>
  31. /// <exception cref="ArgumentException"/>
  32. /// <exception cref="ArgumentNullException"/>
  33. /// <exception cref="DirectoryNotFoundException"/>
  34. /// <exception cref="IOException"/>
  35. /// <exception cref="NotSupportedException"/>
  36. /// <exception cref="UnauthorizedAccessException"/>
  37. [SecurityCritical]
  38. public void Decrypt()
  39. {
  40. Directory.EncryptDecryptDirectoryCore(LongFullName, false, false, PathFormat.LongFullPath);
  41. }
  42. /// <summary>[AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.</summary>
  43. /// <exception cref="ArgumentException"/>
  44. /// <exception cref="ArgumentNullException"/>
  45. /// <exception cref="DirectoryNotFoundException"/>
  46. /// <exception cref="IOException"/>
  47. /// <exception cref="NotSupportedException"/>
  48. /// <exception cref="UnauthorizedAccessException"/>
  49. /// <param name="recursive"><see langword="true"/> to decrypt the directory recursively. <see langword="false"/> only decrypt files and directories in the root of the directory.</param>
  50. [SecurityCritical]
  51. public void Decrypt(bool recursive)
  52. {
  53. Directory.EncryptDecryptDirectoryCore(LongFullName, false, recursive, PathFormat.LongFullPath);
  54. }
  55. #endregion // Decrypt
  56. #region DisableEncryption
  57. /// <summary>[AlphaFS] Disables encryption of the specified directory and the files in it. It does not affect encryption of subdirectories below the indicated directory.</summary>
  58. /// <returns><see langword="true"/> on success, <see langword="false"/> otherwise.</returns>
  59. /// <remarks>This method will create/change the file "Desktop.ini" and wil set Encryption value: "Disable=0"</remarks>
  60. [SecurityCritical]
  61. public void DisableEncryption()
  62. {
  63. Directory.EnableDisableEncryptionCore(LongFullName, false, PathFormat.LongFullPath);
  64. }
  65. #endregion // DisableEncryption
  66. #region EnableEncryption
  67. /// <summary>[AlphaFS] Enables encryption of the specified directory and the files in it. It does not affect encryption of subdirectories below the indicated directory.</summary>
  68. /// <returns><see langword="true"/> on success, <see langword="false"/> otherwise.</returns>
  69. /// <remarks>This method will create/change the file "Desktop.ini" and wil set Encryption value: "Disable=1"</remarks>
  70. [SecurityCritical]
  71. public void EnableEncryption()
  72. {
  73. Directory.EnableDisableEncryptionCore(LongFullName, true, PathFormat.LongFullPath);
  74. }
  75. #endregion // EnableEncryption
  76. #region Encrypt
  77. /// <summary>[AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.</summary>
  78. /// <exception cref="ArgumentException"/>
  79. /// <exception cref="ArgumentNullException"/>
  80. /// <exception cref="DirectoryNotFoundException"/>
  81. /// <exception cref="IOException"/>
  82. /// <exception cref="NotSupportedException"/>
  83. /// <exception cref="UnauthorizedAccessException"/>
  84. [SecurityCritical]
  85. public void Encrypt()
  86. {
  87. Directory.EncryptDecryptDirectoryCore(LongFullName, true, false, PathFormat.LongFullPath);
  88. }
  89. /// <summary>[AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.</summary>
  90. /// <exception cref="ArgumentException"/>
  91. /// <exception cref="ArgumentNullException"/>
  92. /// <exception cref="DirectoryNotFoundException"/>
  93. /// <exception cref="IOException"/>
  94. /// <exception cref="NotSupportedException"/>
  95. /// <exception cref="UnauthorizedAccessException"/>
  96. /// <param name="recursive"><see langword="true"/> to encrypt the directory recursively. <see langword="false"/> only encrypt files and directories in the root of the directory.</param>
  97. [SecurityCritical]
  98. public void Encrypt(bool recursive)
  99. {
  100. Directory.EncryptDecryptDirectoryCore(LongFullName, true, recursive, PathFormat.LongFullPath);
  101. }
  102. #endregion // Encrypt
  103. #endregion // AlphaFS
  104. }
  105. }