fCraft.Paths.FileExists C# (CSharp) Method

FileExists() public static method

Checks whether the file exists in a specified way (case-sensitive or case-insensitive)
public static FileExists ( [ fileName, bool caseSensitive ) : bool
fileName [ filename in question
caseSensitive bool Whether check should be case-sensitive or case-insensitive.
return bool
        public static bool FileExists( [NotNull] string fileName, bool caseSensitive )
        {
            if ( fileName == null )
                throw new ArgumentNullException( "fileName" );
            if ( caseSensitive == MonoCompat.IsCaseSensitive ) {
                return File.Exists( fileName );
            } else {
                return new FileInfo( fileName ).Exists( caseSensitive );
            }
        }

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::FileExists