Standardize() public static method

public static Standardize ( string fileNameToFix, string relativePath ) : string
fileNameToFix string
relativePath string
return string
Beispiel #1
0
        public static string GetDirectory(string fileName, RelativeType relativeType)
        {
            int lastIndex = System.Math.Max(
                fileName.LastIndexOf('/'), fileName.LastIndexOf('\\'));

            if (lastIndex == fileName.Length - 1)
            {
                // If this happens then fileName is actually a directory.
                // So we should return the parent directory of the argument.



                lastIndex = System.Math.Max(
                    fileName.LastIndexOf('/', fileName.Length - 2),
                    fileName.LastIndexOf('\\', fileName.Length - 2));
            }

            if (lastIndex != -1)
            {
                bool isFtp = false;

#if !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE && !MONOGAME
                isFtp = FtpManager.IsFtp(fileName);
#endif

                if (FileManager.IsUrl(fileName) || isFtp)
                {
                    // don't standardize URLs - they're case sensitive!!!
                    return(fileName.Substring(0, lastIndex + 1));
                }
                else
                {
                    if (relativeType == RelativeType.Absolute)
                    {
                        return(FileManager.Standardize(fileName.Substring(0, lastIndex + 1), null));
                    }
                    else
                    {
                        return(FileManager.Standardize(fileName.Substring(0, lastIndex + 1), ""));
                    }
                }
            }
            else
            {
                return("");                // there was no directory found.
            }
        }
Beispiel #2
0
        public static string MakeRelative(string pathToMakeRelative, string pathToMakeRelativeTo)
        {
            if (string.IsNullOrEmpty(pathToMakeRelative) == false)
            {
                pathToMakeRelative   = FileManager.Standardize(pathToMakeRelative, null);
                pathToMakeRelativeTo = FileManager.Standardize(pathToMakeRelativeTo, null);
                if (!pathToMakeRelativeTo.EndsWith("/"))
                {
                    pathToMakeRelativeTo += "/";
                }

                // Use the old method if we can
                if (pathToMakeRelative.ToLowerInvariant().StartsWith(pathToMakeRelativeTo.ToLowerInvariant()))
                {
                    pathToMakeRelative = pathToMakeRelative.Substring(pathToMakeRelativeTo.Length);
                }
                else
                {
                    // Otherwise, we have to use the new method to identify the common root

                    // Split the path strings
                    string[] path    = pathToMakeRelative.ToLowerInvariant().Split('/');
                    string[] relpath = pathToMakeRelativeTo.ToLowerInvariant().Split('/');

                    string relativepath = string.Empty;

                    // build the new path
                    int start = 0;
                    // November 1, 2011
                    // Do we want to do this:
                    // March 26, 2012
                    // Yes!  Found a bug
                    // while working on wahoo's
                    // tools that we need to check
                    // "start" against the length of
                    // the string arrays.
                    //while (start < path.Length && start < relpath.Length && path[start] == relpath[start])
                    //while (path[start] == relpath[start])
                    while (start < path.Length && start < relpath.Length && path[start] == relpath[start])
                    {
                        start++;
                    }

                    // If start is 0, they aren't on the same drive, so there is no way to make the path relative without it being absolute
                    if (start != 0)
                    {
                        // add .. for every directory left in the relative path, this is the shared root
                        for (int i = start; i < relpath.Length; i++)
                        {
                            if (relpath[i] != string.Empty)
                            {
                                relativepath += @"../";
                            }
                        }

                        // if the current relative path is still empty, and there are more than one entries left in the path,
                        // the file is in a subdirectory.  Start with ./
                        if (relativepath == string.Empty && path.Length - start > 0)
                        {
                            relativepath += @"./";
                        }

                        // add the rest of the path
                        for (int i = start; i < path.Length; i++)
                        {
                            relativepath += path[i];
                            if (i < path.Length - 1)
                            {
                                relativepath += "/";
                            }
                        }

                        pathToMakeRelative = relativepath;
                    }
                }
            }
            return(pathToMakeRelative);
        }