Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

139 linhas
5.2 KiB

  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.Text;
  23. namespace Alphaleonis.Win32.Filesystem
  24. {
  25. /// <summary>Information about an alternate data stream.</summary>
  26. /// <seealso cref="O:Alphaleonis.Win32.Filesystem.File.EnumerateAlternateDataStreams"/>
  27. public struct AlternateDataStreamInfo
  28. {
  29. #region Constructor
  30. internal AlternateDataStreamInfo(string fullPath, NativeMethods.WIN32_FIND_STREAM_DATA findData) : this()
  31. {
  32. StreamName = ParseStreamName(findData.cStreamName);
  33. Size = findData.StreamSize;
  34. _fullPath = fullPath;
  35. }
  36. #endregion // Constructor
  37. #region Public Properties
  38. /// <summary>Gets the name of the alternate data stream.</summary>
  39. /// <remarks>This value is an empty string for the default stream (:$DATA), and for any other data stream it contains the name of the stream.</remarks>
  40. /// <value>The name of the stream.</value>
  41. public string StreamName { get; private set; }
  42. /// <summary>Gets the size of the stream.</summary>
  43. public long Size { get; private set; }
  44. private readonly string _fullPath;
  45. /// <summary>Gets the full path to the stream.</summary>
  46. /// <remarks>
  47. /// This is a path in long path format that can be passed to <see cref="O:Alphaleonis.Win32.Filesystem.File.Open"/> to open the stream if
  48. /// <see cref="PathFormat.FullPath"/> or
  49. /// <see cref="PathFormat.LongFullPath"/> is specified.
  50. /// </remarks>
  51. /// <value>The full path to the stream in long path format.</value>
  52. public string FullPath
  53. {
  54. get { return _fullPath + Path.StreamSeparator + StreamName + Path.StreamDataLabel; }
  55. }
  56. #endregion // Public Properties
  57. #region Public Methods
  58. /// <summary>Returns the hash code for this instance.</summary>
  59. /// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
  60. public override int GetHashCode()
  61. {
  62. return StreamName.GetHashCode();
  63. }
  64. /// <summary>Indicates whether this instance and a specified object are equal.</summary>
  65. /// <param name="obj">The object to compare with the current instance.</param>
  66. /// <returns>
  67. /// true if <paramref name="obj" /> and this instance are the same type and represent the same value; otherwise, false.
  68. /// </returns>
  69. public override bool Equals(object obj)
  70. {
  71. if (obj is AlternateDataStreamInfo)
  72. {
  73. AlternateDataStreamInfo other = (AlternateDataStreamInfo) obj;
  74. return StreamName.Equals(other.StreamName, StringComparison.OrdinalIgnoreCase) && Size.Equals(other.Size);
  75. }
  76. return false;
  77. }
  78. /// <summary>Equality operator.</summary>
  79. /// <param name="first">The first operand.</param>
  80. /// <param name="second">The second operand.</param>
  81. /// <returns>The result of the operation.</returns>
  82. public static bool operator ==(AlternateDataStreamInfo first, AlternateDataStreamInfo second)
  83. {
  84. return first.Equals(second);
  85. }
  86. /// <summary>Inequality operator.</summary>
  87. /// <param name="first">The first operand.</param>
  88. /// <param name="second">The second operand.</param>
  89. /// <returns>The result of the operation.</returns>
  90. public static bool operator !=(AlternateDataStreamInfo first, AlternateDataStreamInfo second)
  91. {
  92. return !first.Equals(second);
  93. }
  94. #endregion // Public Methods
  95. #region Private Methods
  96. private static string ParseStreamName(string input)
  97. {
  98. if (input == null || input.Length < 2)
  99. return string.Empty;
  100. if (input[0] != Path.StreamSeparatorChar)
  101. throw new ArgumentException(Resources.Invalid_Stream_Name);
  102. var sb = new StringBuilder();
  103. for (int i = 1; i < input.Length; i++)
  104. {
  105. if (input[i] == Path.StreamSeparatorChar)
  106. break;
  107. sb.Append(input[i]);
  108. }
  109. return sb.ToString();
  110. }
  111. #endregion // Private Methods
  112. }
  113. }