fCraft.Paths.FindFiles C# (CSharp) Method

FindFiles() public static method

Find files that match the name in a case-insensitive way.
public static FindFiles ( [ fullFileName ) : System.IO.FileInfo[]
fullFileName [ Case-insensitive filename to look for.
return System.IO.FileInfo[]
        public static FileInfo[] FindFiles( [NotNull] string fullFileName )
        {
            if ( fullFileName == null )
                throw new ArgumentNullException( "fullFileName" );
            FileInfo fi = new FileInfo( fullFileName );
            DirectoryInfo parentDir = fi.Directory;
            return parentDir.GetFiles( "*", SearchOption.TopDirectoryOnly )
                            .Where( file => file.Name.Equals( fi.Name, StringComparison.OrdinalIgnoreCase ) )
                            .ToArray();
        }

Usage Example

Example #1
0
        // Makes sure that the map file exists, is properly named, and is loadable.
        static void CheckMapFile([NotNull] string worldName)
        {
            if (worldName == null)
            {
                throw new ArgumentNullException("worldName");
            }

            // Check the world's map file
            string fullMapFileName = Path.Combine(Paths.MapPath, worldName + ".fcm");
            string fileName        = Path.GetFileName(fullMapFileName);

            if (Paths.FileExists(fullMapFileName, false))
            {
                if (!Paths.FileExists(fullMapFileName, true))
                {
                    // Map file has wrong capitalization
                    FileInfo[] matches = Paths.FindFiles(fullMapFileName);
                    if (matches.Length == 1)
                    {
                        // Try to rename the map file to match world's capitalization
                        Paths.ForceRename(matches[0].FullName, fileName);
                        if (Paths.FileExists(fullMapFileName, true))
                        {
                            Logger.Log(LogType.Warning,
                                       "WorldManager.CheckMapFile: Map file for world \"{0}\" was renamed from \"{1}\" to \"{2}\"",
                                       worldName, matches[0].Name, fileName);
                        }
                        else
                        {
                            Logger.Log(LogType.Error,
                                       "WorldManager.CheckMapFile: Failed to rename map file of \"{0}\" from \"{1}\" to \"{2}\"",
                                       worldName, matches[0].Name, fileName);
                            return;
                        }
                    }
                    else
                    {
                        Logger.Log(LogType.Warning,
                                   "WorldManager.CheckMapFile: More than one map file exists matching the world name \"{0}\". " +
                                   "Please check the map directory and use /WLoad to load the correct file.",
                                   worldName);
                        return;
                    }
                }
                // Try loading the map header
                try {
                    MapUtility.LoadHeader(fullMapFileName);
                } catch (Exception ex) {
                    Logger.Log(LogType.Warning,
                               "WorldManager.CheckMapFile: Could not load map file for world \"{0}\": {1}",
                               worldName, ex);
                }
            }
            else
            {
                Logger.Log(LogType.Warning,
                           "WorldManager.CheckMapFile: Map file for world \"{0}\" was not found.",
                           worldName);
            }
        }
All Usage Examples Of fCraft.Paths::FindFiles