/src/AlphaFS/Device/Volume/Volume.VolumeLabel.cs

https://github.com/alphaleonis/AlphaFS · C# · 114 lines · 48 code · 19 blank · 47 comment · 4 complexity · e4ac97af276f32d95491f7936f3b410e MD5 · raw file

  1. /* Copyright (C) 2008-2018 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.Runtime.InteropServices;
  23. using System.Security;
  24. namespace Alphaleonis.Win32.Filesystem
  25. {
  26. public static partial class Volume
  27. {
  28. /// <summary>[AlphaFS] Deletes the label of the file system volume that is the root of the current directory.</summary>
  29. [SecurityCritical]
  30. public static void DeleteCurrentVolumeLabel()
  31. {
  32. SetVolumeLabel(null, null);
  33. }
  34. /// <summary>[AlphaFS] Deletes the label of a file system volume.</summary>
  35. /// <exception cref="ArgumentNullException"/>
  36. /// <param name="rootPathName">The root directory of a file system volume. This is the volume the function will remove the label.</param>
  37. [SecurityCritical]
  38. public static void DeleteVolumeLabel(string rootPathName)
  39. {
  40. if (Utils.IsNullOrWhiteSpace(rootPathName))
  41. throw new ArgumentNullException("rootPathName");
  42. SetVolumeLabel(rootPathName, null);
  43. }
  44. /// <summary>[AlphaFS] Retrieve the label of a file system volume.</summary>
  45. /// <param name="volumePath">
  46. /// A path to a volume. For example: "C:\", "\\server\share", or "\\?\Volume{c0580d5e-2ad6-11dc-9924-806e6f6e6963}\".
  47. /// </param>
  48. /// <returns>The the label of the file system volume. This function can return <c>string.Empty</c> since a volume label is generally not mandatory.</returns>
  49. [SecurityCritical]
  50. public static string GetVolumeLabel(string volumePath)
  51. {
  52. return new VolumeInfo(volumePath, true, true).Name;
  53. }
  54. /// <summary>[AlphaFS] Sets the label of the file system volume that is the root of the current directory.</summary>
  55. /// <exception cref="ArgumentNullException"/>
  56. /// <param name="volumeName">A name for the volume.</param>
  57. [SecurityCritical]
  58. public static void SetCurrentVolumeLabel(string volumeName)
  59. {
  60. if (Utils.IsNullOrWhiteSpace(volumeName))
  61. throw new ArgumentNullException("volumeName");
  62. var success = NativeMethods.SetVolumeLabel(null, volumeName);
  63. var lastError = Marshal.GetLastWin32Error();
  64. if (!success)
  65. NativeError.ThrowException(lastError, volumeName);
  66. }
  67. /// <summary>[AlphaFS] Sets the label of a file system volume.</summary>
  68. /// <param name="volumePath">
  69. /// <para>A path to a volume. For example: "C:\", "\\server\share", or "\\?\Volume{c0580d5e-2ad6-11dc-9924-806e6f6e6963}\"</para>
  70. /// <para>If this parameter is <c>null</c>, the function uses the current drive.</para>
  71. /// </param>
  72. /// <param name="volumeName">
  73. /// <para>A name for the volume.</para>
  74. /// <para>If this parameter is <c>null</c>, the function deletes any existing label</para>
  75. /// <para>from the specified volume and does not assign a new label.</para>
  76. /// </param>
  77. [SecurityCritical]
  78. public static void SetVolumeLabel(string volumePath, string volumeName)
  79. {
  80. // rootPathName == null is allowed, means current drive.
  81. // Setting volume label only applies to Logical Drives pointing to local resources.
  82. //if (!Path.IsLocalPath(rootPathName))
  83. //return false;
  84. volumePath = Path.AddTrailingDirectorySeparator(volumePath, false);
  85. // NTFS uses a limit of 32 characters for the volume label as of Windows Server 2003.
  86. using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors))
  87. {
  88. var success = NativeMethods.SetVolumeLabel(volumePath, volumeName);
  89. var lastError = Marshal.GetLastWin32Error();
  90. if (!success)
  91. NativeError.ThrowException(lastError, volumePath, volumeName);
  92. }
  93. }
  94. }
  95. }