public VolumeInfo(string volumeName) { if (Utils.IsNullOrWhiteSpace(volumeName)) { throw new ArgumentNullException("volumeName"); } if (volumeName.Length == 1) { Name += Path.VolumeSeparatorChar; } else { Name = Path.GetPathRoot(volumeName, false); } if (Utils.IsNullOrWhiteSpace(Name)) { throw new ArgumentException("Argument must be a drive letter (\"C\"), RootDir (\"C:\\\") or UNC path (\"\\\\server\\share\")"); } // If an exception is thrown, the original drivePath is used. Name = Path.AddTrailingDirectorySeparator(Name, false); _volumeHandle = null; }
public VolumeInfo(string volumeName) { if (Utils.IsNullOrWhiteSpace(volumeName)) { throw new ArgumentNullException("volumeName"); } if (!volumeName.StartsWith(Path.LongPathPrefix, StringComparison.Ordinal)) { volumeName = Path.IsUncPathCore(volumeName, false, false) ? Path.GetLongPathCore(volumeName, GetFullPathOptions.None) : Path.LongPathPrefix + volumeName; } else { volumeName = volumeName.Length == 1 ? volumeName + Path.VolumeSeparatorChar : Path.GetPathRoot(volumeName, false); if (!volumeName.StartsWith(Path.GlobalRootPrefix, StringComparison.OrdinalIgnoreCase)) { volumeName = Path.GetPathRoot(volumeName, false); } } if (Utils.IsNullOrWhiteSpace(volumeName)) { throw new ArgumentException(Resources.InvalidDriveLetterArgument, "volumeName"); } Name = Path.AddTrailingDirectorySeparator(volumeName, false); _volumeHandle = null; }
public DiskSpaceInfo(string drivePath) { if (Utils.IsNullOrWhiteSpace(drivePath)) { throw new ArgumentNullException("drivePath"); } if (drivePath.Length == 1) { DriveName += Path.VolumeSeparatorChar; } else { DriveName = Path.GetPathRoot(drivePath, false); } if (Utils.IsNullOrWhiteSpace(DriveName)) { throw new ArgumentException("Argument must be a drive letter (\"C\"), RootDir (\"C:\\\") or UNC path (\"\\\\server\\share\")"); } // MSDN: // If this parameter is a UNC name, it must include a trailing backslash (for example, "\\MyServer\MyShare\"). // Furthermore, a drive specification must have a trailing backslash (for example, "C:\"). // The calling application must have FILE_LIST_DIRECTORY access rights for this directory. DriveName = Path.AddTrailingDirectorySeparator(DriveName, false); }
public static IEnumerable <string> EnumerateVolumePathNames(string volumeGuid) { if (Utils.IsNullOrWhiteSpace(volumeGuid)) { throw new ArgumentNullException("volumeGuid"); } if (!volumeGuid.StartsWith(Path.VolumePrefix + "{", StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException(Resources.Not_A_Valid_Guid, "volumeGuid"); } var volName = Path.AddTrailingDirectorySeparator(volumeGuid, false); uint requiredLength = 10; var cBuffer = new char[requiredLength]; using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors)) while (!NativeMethods.GetVolumePathNamesForVolumeName(volName, cBuffer, (uint)cBuffer.Length, out requiredLength)) { var lastError = Marshal.GetLastWin32Error(); switch ((uint)lastError) { case Win32Errors.ERROR_MORE_DATA: case Win32Errors.ERROR_INSUFFICIENT_BUFFER: cBuffer = new char[requiredLength]; break; default: NativeError.ThrowException(lastError, volumeGuid); break; } } var buffer = new StringBuilder(cBuffer.Length); foreach (var c in cBuffer) { if (c != Path.StringTerminatorChar) { buffer.Append(c); } else { if (buffer.Length > 0) { yield return(buffer.ToString()); buffer.Length = 0; } } } }
public static IEnumerable <string> EnumerateVolumeMountPoints(string volumeGuid) { if (Utils.IsNullOrWhiteSpace(volumeGuid)) { throw new ArgumentNullException("volumeGuid"); } if (!volumeGuid.StartsWith(Path.VolumePrefix + "{", StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException(Resources.Not_A_Valid_Guid, "volumeGuid"); } // A trailing backslash is required. volumeGuid = Path.AddTrailingDirectorySeparator(volumeGuid, false); var buffer = new StringBuilder(NativeMethods.MaxPathUnicode); using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors)) using (var handle = NativeMethods.FindFirstVolumeMountPoint(volumeGuid, buffer, (uint)buffer.Capacity)) { var lastError = Marshal.GetLastWin32Error(); if (!NativeMethods.IsValidHandle(handle, false)) { switch ((uint)lastError) { case Win32Errors.ERROR_NO_MORE_FILES: case Win32Errors.ERROR_PATH_NOT_FOUND: // Observed with USB stick, FAT32 formatted. yield break; default: NativeError.ThrowException(lastError, volumeGuid); break; } } yield return(buffer.ToString()); while (NativeMethods.FindNextVolumeMountPoint(handle, buffer, (uint)buffer.Capacity)) { lastError = Marshal.GetLastWin32Error(); var throwException = lastError != Win32Errors.ERROR_NO_MORE_FILES && lastError != Win32Errors.ERROR_PATH_NOT_FOUND && lastError != Win32Errors.ERROR_MORE_DATA; if (!NativeMethods.IsValidHandle(handle, lastError, volumeGuid, throwException)) { yield break; } yield return(buffer.ToString()); } } }
public static string GetVolumeGuid(string volumeMountPoint) { if (Utils.IsNullOrWhiteSpace(volumeMountPoint)) { throw new ArgumentNullException("volumeMountPoint"); } // The string must end with a trailing backslash ('\'). volumeMountPoint = Path.GetFullPathCore(null, volumeMountPoint, GetFullPathOptions.AsLongPath | GetFullPathOptions.AddTrailingDirectorySeparator | GetFullPathOptions.FullCheck); var volumeGuid = new StringBuilder(100); var uniqueName = new StringBuilder(100); try { using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors)) { // GetVolumeNameForVolumeMountPoint() // 2013-07-18: MSDN does not confirm LongPath usage but a Unicode version of this function exists. return(NativeMethods.GetVolumeNameForVolumeMountPoint(volumeMountPoint, volumeGuid, (uint)volumeGuid.Capacity) // The string must end with a trailing backslash. ? NativeMethods.GetVolumeNameForVolumeMountPoint(Path.AddTrailingDirectorySeparator(volumeGuid.ToString(), false), uniqueName, (uint)uniqueName.Capacity) ? uniqueName.ToString() : null : null); } } finally { var lastError = (uint)Marshal.GetLastWin32Error(); switch (lastError) { case Win32Errors.ERROR_INVALID_NAME: NativeError.ThrowException(lastError, volumeMountPoint); break; case Win32Errors.ERROR_MORE_DATA: // (1) When GetVolumeNameForVolumeMountPoint() succeeds, lastError is set to Win32Errors.ERROR_MORE_DATA. break; default: // (2) When volumeMountPoint is a network drive mapping or UNC path, lastError is set to Win32Errors.ERROR_INVALID_PARAMETER. // Throw IOException. NativeError.ThrowException(lastError, volumeMountPoint); break; } } }
public static DriveType GetDriveType(string drivePath) { // drivePath is allowed to be == null. drivePath = Path.AddTrailingDirectorySeparator(drivePath, false); // ChangeErrorMode is for the Win32 SetThreadErrorMode() method, used to suppress possible pop-ups. using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors)) return(NativeMethods.GetDriveType(drivePath)); }
public static void SetVolumeMountPoint(string volumeMountPoint, string volumeGuid) { if (Utils.IsNullOrWhiteSpace(volumeMountPoint)) { throw new ArgumentNullException("volumeMountPoint"); } if (Utils.IsNullOrWhiteSpace(volumeGuid)) { throw new ArgumentNullException("volumeGuid"); } if (!volumeGuid.StartsWith(Path.VolumePrefix + "{", StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException(Resources.Not_A_Valid_Guid, "volumeGuid"); } volumeMountPoint = Path.GetFullPathCore(null, volumeMountPoint, GetFullPathOptions.AsLongPath | GetFullPathOptions.AddTrailingDirectorySeparator | GetFullPathOptions.FullCheck); // This string must be of the form "\\?\Volume{GUID}\" volumeGuid = Path.AddTrailingDirectorySeparator(volumeGuid, false); // ChangeErrorMode is for the Win32 SetThreadErrorMode() method, used to suppress possible pop-ups. using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors)) { // SetVolumeMountPoint() // 2014-01-29: MSDN does not confirm LongPath usage but a Unicode version of this function exists. // The string must end with a trailing backslash. var success = NativeMethods.SetVolumeMountPoint(volumeMountPoint, volumeGuid); var lastError = Marshal.GetLastWin32Error(); if (!success) { // If the lpszVolumeMountPoint parameter contains a path to a mounted folder, // GetLastError returns ERROR_DIR_NOT_EMPTY, even if the directory is empty. if (lastError != Win32Errors.ERROR_DIR_NOT_EMPTY) { NativeError.ThrowException(lastError, volumeGuid); } } } }
public DiskSpaceInfo(string drivePath) { if (Utils.IsNullOrWhiteSpace(drivePath)) { throw new ArgumentNullException("drivePath"); } drivePath = drivePath.Length == 1 ? drivePath + Path.VolumeSeparatorChar : Path.GetPathRoot(drivePath, false); if (Utils.IsNullOrWhiteSpace(drivePath)) { throw new ArgumentException(Resources.InvalidDriveLetterArgument, "drivePath"); } // MSDN: // If this parameter is a UNC name, it must include a trailing backslash (for example, "\\MyServer\MyShare\"). // Furthermore, a drive specification must have a trailing backslash (for example, "C:\"). // The calling application must have FILE_LIST_DIRECTORY access rights for this directory. DriveName = Path.AddTrailingDirectorySeparator(drivePath, false); }
public static void SetVolumeLabel(string volumePath, string volumeName) { // rootPathName == null is allowed, means current drive. // Setting volume label only applies to Logical Drives pointing to local resources. //if (!Path.IsLocalPath(rootPathName)) //return false; volumePath = Path.AddTrailingDirectorySeparator(volumePath, false); // NTFS uses a limit of 32 characters for the volume label as of Windows Server 2003. using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors)) { var success = NativeMethods.SetVolumeLabel(volumePath, volumeName); var lastError = Marshal.GetLastWin32Error(); if (!success) { NativeError.ThrowException(lastError, volumePath, volumeName); } } }
internal static NativeMethods.RemoteNameInfo GetRemoteNameInfoInternal(string path, bool continueOnException) { if (Utils.IsNullOrWhiteSpace(path)) { throw new ArgumentNullException("path"); } path = Path.GetRegularPathInternal(path, GetFullPathOptions.CheckInvalidPathChars); // If path already is a network share path, we fill the RemoteNameInfo structure ourselves. if (Path.IsUncPath(path, false)) { return new NativeMethods.RemoteNameInfo { UniversalName = Path.AddTrailingDirectorySeparator(path, false), ConnectionName = Path.RemoveTrailingDirectorySeparator(path, false), RemainingPath = Path.DirectorySeparator } } ; // Use large enough buffer to prevent a 2nd call. uint bufferSize = 1024; var buffer = new IntPtr(bufferSize); try { uint lastError; do { // Allocate the memory. buffer = Marshal.AllocHGlobal((int)bufferSize); // Structure: UNIVERSAL_NAME_INFO_LEVEL = 1 (not used in AlphaFS). // Structure: REMOTE_NAME_INFO_LEVEL = 2 lastError = NativeMethods.WNetGetUniversalName(path, 2, buffer, out bufferSize); switch (lastError) { case Win32Errors.NO_ERROR: return(Utils.MarshalPtrToStructure <NativeMethods.RemoteNameInfo>(0, buffer)); case Win32Errors.ERROR_MORE_DATA: //bufferSize = Received the required buffer size, retry. if (buffer != IntPtr.Zero) { Marshal.FreeHGlobal(buffer); } break; } } while (lastError == Win32Errors.ERROR_MORE_DATA); if (!continueOnException && lastError != Win32Errors.NO_ERROR) { throw new NetworkInformationException((int)lastError); } // Return an empty structure (all fields set to null). return(new NativeMethods.RemoteNameInfo()); } finally { if (buffer != IntPtr.Zero) { Marshal.FreeHGlobal(buffer); } } }