Esempio n. 1
0
        public static bool PathFileExists(string path)
        {
            // PathFileExists()
            // 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists.

            return(!Utils.IsNullOrWhiteSpace(path) && NativeMethods.PathFileExists(Path.GetFullPathCore(null, path, GetFullPathOptions.AsLongPath | GetFullPathOptions.FullCheck | GetFullPathOptions.ContinueOnNonExist)));
        }
Esempio n. 2
0
        public static bool PathFileExists(string path)
        {
            // PathFileExists()
            // In the ANSI version of this function, the name is limited to 248 characters.
            // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
            // 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists.

            return(!Utils.IsNullOrWhiteSpace(path) && NativeMethods.PathFileExists(Path.GetFullPathCore(null, path, GetFullPathOptions.AsLongPath | GetFullPathOptions.FullCheck | GetFullPathOptions.ContinueOnNonExist)));
        }
        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 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);
                    }
                }
            }
        }
Esempio n. 5
0
        public static string GetVolumePathName(string path)
        {
            if (Utils.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException("path");
            }


            using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors))
            {
                var volumeRootPath = new StringBuilder(NativeMethods.MaxPathUnicode / 32);
                var pathLp         = Path.GetFullPathCore(null, path, GetFullPathOptions.AsLongPath | GetFullPathOptions.FullCheck);


                // GetVolumePathName()
                // 2013-07-18: MSDN does not confirm LongPath usage but a Unicode version of this function exists.

                var success = NativeMethods.GetVolumePathName(pathLp, volumeRootPath, (uint)volumeRootPath.Capacity);

                var lastError = Marshal.GetLastWin32Error();

                if (success)
                {
                    return(Path.GetRegularPathCore(volumeRootPath.ToString(), GetFullPathOptions.None, false));
                }


                switch ((uint)lastError)
                {
                // Don't throw exception on these errors.
                case Win32Errors.ERROR_NO_MORE_FILES:
                case Win32Errors.ERROR_INVALID_PARAMETER:
                case Win32Errors.ERROR_INVALID_NAME:
                    break;

                default:
                    NativeError.ThrowException(lastError, path);
                    break;
                }


                // Return original path.
                return(path);
            }
        }