fCraft.Paths.IsValidPath C# (CSharp) Method

IsValidPath() public static method

public static IsValidPath ( string path ) : bool
path string
return bool
        public static bool IsValidPath( string path )
        {
            try {
                new FileInfo( path );
                return true;
            } catch ( ArgumentException ) {
            } catch ( PathTooLongException ) {
            } catch ( NotSupportedException ) {
            }
            return false;
        }

Usage Example

Example #1
0
        public static string FindMapFile([NotNull] Player player, [NotNull] string fileName)
        {
            if (player == null)
            {
                throw new ArgumentNullException("player");
            }
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            // Check if path contains missing drives or invalid characters
            if (!Paths.IsValidPath(fileName))
            {
                player.Message("Invalid filename or path.");
                return(null);
            }

            // Look for the file
            string sourceFullFileName = Path.Combine(Paths.MapPath, fileName);

            if (!File.Exists(sourceFullFileName) && !Directory.Exists(sourceFullFileName))
            {
                if (File.Exists(sourceFullFileName + ".fcm"))
                {
                    // Try with extension added
                    sourceFullFileName += ".fcm";
                }
                else if (MonoCompat.IsCaseSensitive)
                {
                    try {
                        // If we're on a case-sensitive OS, try case-insensitive search
                        FileInfo[] candidates = Paths.FindFiles(sourceFullFileName + ".fcm");
                        if (candidates.Length == 0)
                        {
                            candidates = Paths.FindFiles(sourceFullFileName);
                        }

                        if (candidates.Length == 0)
                        {
                            player.Message("File/directory not found: {0}", fileName);
                        }
                        else if (candidates.Length == 1)
                        {
                            player.Message("Filenames are case-sensitive! Did you mean to load \"{0}\"?", candidates[0].Name);
                        }
                        else
                        {
                            player.Message("Filenames are case-sensitive! Did you mean to load one of these: {0}",
                                           String.Join(", ", candidates.Select(c => c.Name).ToArray()));
                        }
                    } catch (DirectoryNotFoundException ex) {
                        player.Message(ex.Message);
                    }
                    return(null);
                }
                else
                {
                    // Nothing found!
                    player.Message("File/directory not found: {0}", fileName);
                    return(null);
                }
            }

            // Make sure that the given file is within the map directory
            if (!Paths.Contains(Paths.MapPath, sourceFullFileName))
            {
                player.MessageUnsafePath();
                return(null);
            }

            return(sourceFullFileName);
        }
All Usage Examples Of fCraft.Paths::IsValidPath