コード例 #1
0
ファイル: FileManager.cs プロジェクト: Aethere21/Pokamen
        public static bool IsRelativeTo(string fileName, string directory)
        {
            if (!IsRelative(fileName))
            {
                // the filename is an absolute path

                if (!IsRelative(directory))
                {
                    // just have to make sure that the filename includes the path
                    fileName  = fileName.ToLower().Replace('\\', '/');
                    directory = directory.ToLower().Replace('\\', '/');

                    return(fileName.IndexOf(directory) == 0);
                }
            }
            else // fileName is relative
            {
                if (IsRelative(directory))
                {
                    // both are relative, so let's make em full and see what happens
                    string fullFileName  = FileManager.Standardize(fileName);
                    string fullDirectory = FileManager.Standardize(directory);

                    return(IsRelativeTo(fullFileName, fullDirectory));
                }
            }
            return(false);
        }
コード例 #2
0
ファイル: FileManager.cs プロジェクト: Aethere21/Pokamen
        public static void GetAllFilesInDirectory(string directory, string fileType, int depthToSearch, List <string> arrayToReturn)
        {
            if (!Directory.Exists(directory))
            {
                return;
            }
            //if (directory == "")
            //    directory = mRelativeDirectory;

            if (directory.EndsWith(@"\") == false && directory.EndsWith("/") == false)
            {
                directory += @"\";
            }

            // if they passed in a fileType which begins with a period (like ".jpg"), then
            // remove the period so only the extension remains.  That is, convert
            // ".jpg" to "jpg"
            if (fileType != null && fileType.Length > 0 && fileType[0] == '.')
            {
                fileType = fileType.Substring(1);
            }

            string[] files       = System.IO.Directory.GetFiles(directory);
            string[] directories = System.IO.Directory.GetDirectories(directory);

            if (string.IsNullOrEmpty(fileType))
            {
                for (int i = 0; i < files.Length; i++)
                {
                    files[i] = FileManager.Standardize(files[i]);
                }
                arrayToReturn.AddRange(files);
            }
            else
            {
                int fileCount = files.Length;

                for (int i = 0; i < fileCount; i++)
                {
                    string file = files[i];
                    if (GetExtension(file) == fileType)
                    {
                        arrayToReturn.Add(file);
                    }
                }
            }


            if (depthToSearch > 0)
            {
                int directoryCount = directories.Length;
                for (int i = 0; i < directoryCount; i++)
                {
                    string directoryChecking = directories[i];

                    GetAllFilesInDirectory(directoryChecking, fileType, depthToSearch - 1, arrayToReturn);
                }
            }
        }
コード例 #3
0
ファイル: FileManager.cs プロジェクト: Aethere21/Pokamen
        public static string MakeRelative(string pathToMakeRelative, string pathToMakeRelativeTo, bool preserveCase)
        {
            if (string.IsNullOrEmpty(pathToMakeRelative) == false)
            {
                pathToMakeRelative   = FileManager.Standardize(pathToMakeRelative, preserveCase);
                pathToMakeRelativeTo = FileManager.Standardize(pathToMakeRelativeTo, preserveCase);

                // 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.Split('\\');
                    string[] relpath = pathToMakeRelativeTo.Split('\\');

                    string relativepath = string.Empty;

                    // build the new path
                    int start = 0;
                    while (start < path.Length && start < relpath.Length && path[start].ToLower() == relpath[start].ToLower())
                    {
                        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;
                    }
                }
                if (pathToMakeRelative.StartsWith("\\"))
                {
                    pathToMakeRelative = pathToMakeRelative.Substring(1);
                }
            }



            return(pathToMakeRelative);
        }