public static string TrimSlashes(this string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(path);
            }

            var trimmed = path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

            if (MockUnixSupport.IsUnixPlatform() &&
                (path[0] == Path.DirectorySeparatorChar || path[0] == Path.AltDirectorySeparatorChar) &&
                trimmed == "")
            {
                return(Path.DirectorySeparatorChar.ToString());
            }

            if (!MockUnixSupport.IsUnixPlatform() &&
                trimmed.Length == 2 &&
                char.IsLetter(trimmed[0]) &&
                trimmed[1] == ':')
            {
                return(trimmed + Path.DirectorySeparatorChar);
            }

            return(trimmed);
        }
Esempio n. 2
0
        public void IsLegalAbsoluteOrRelative(string path, string paramName)
        {
            if (path == null)
            {
                throw new ArgumentNullException(paramName, StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL"));
            }

            if (path == string.Empty)
            {
                throw new ArgumentException("Empty file name is not legal.", paramName);
            }

            if (path.Trim() == string.Empty)
            {
                throw new ArgumentException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"), paramName);
            }

            if (!MockUnixSupport.IsUnixPlatform())
            {
                if (!IsValidUseOfVolumeSeparatorChar(path))
                {
                    throw new NotSupportedException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"));
                }
            }

            if (ExtractFileName(path).IndexOfAny(_mockFileDataAccessor.Path.GetInvalidFileNameChars()) > -1)
            {
                throw new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"));
            }

            var filePath = ExtractFilePath(path);

            if (MockPath.HasIllegalCharacters(filePath, false))
            {
                throw new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"));
            }
        }