Dans C# vérifier que le nom de fichier est * possibly * valide (pas qu'il existe) [dupliquer]

cette question a déjà une réponse ici:

  • Comment puis-je vérifier si une chaîne de caractères donnée est un nom de fichier légal/valide sous Windows? 25 réponses

y a-t-il une méthode dans le système?Io namespace qui vérifie la validité d'un nom de fichier?

par exemple, C:foobar validerait et :"~-* ne serait pas

ou un peu plus compliqué, X:foobar validerait s'il y a un lecteur X: sur le système, mais ne serait pas autrement.

je suppose que je pourrais écrire une telle méthode moi-même, mais je suis plus intéressé par un intégré.

114
demandé sur will-hart 2009-01-08 00:06:56

14 réponses

Just do;

System.IO.FileInfo fi = null;
try {
  fi = new System.IO.FileInfo(fileName);
}
catch (ArgumentException) { }
catch (System.IO.PathTooLongException) { }
catch (NotSupportedException) { }
if (ReferenceEquals(fi, null)) {
  // file name is not valid
} else {
  // file name is valid... May check for existence by calling fi.Exists.
}

pour créer une instance FileInfo le fichier n'a pas besoin d'exister.

85
répondu mmmmmmmm 2017-04-30 00:51:48

vous pouvez obtenir une liste de caractères invalides à partir du chemin .GetInvalidPathChars et GetInvalidFileNameChars comme indiqué dans à cette question.

comme l'a noté jberger, il y a d'autres caractères qui ne sont pas inclus dans la réponse de cette méthode. Pour plus de détails sur la plate-forme windows, allez à nommer les fichiers, les chemins et les espaces de noms sur MSDN,

comme Micah souligne , il y a annuaire.GetLogicalDrives pour obtenir une liste de disques valides.

33
répondu Eugene Katz 2017-05-23 11:47:22

, Vous pouvez utiliser le Système.Uri de la classe. La classe Uri N'est pas seulement utile pour les URLs web, elle gère aussi les chemins du système de fichiers. L'utilisation de l'Uri.Méthode TryCreate pour trouver si le chemin est enraciné puis utiliser la propriété IsLoopback pour déterminer si L'Uri renvoie à la machine locale.

Voici une méthode simple qui détermine si une chaîne de caractères est un chemin de fichier valide, local et enraciné.

public bool IsPathValidRootedLocal(String pathString) {
    Uri pathUri;
    Boolean isValidUri = Uri.TryCreate(pathString, UriKind.Absolute, out pathUri);
    return isValidUri && pathUri != null && pathUri.IsLoopback;
}

je suis sûr que ça va marcher.

9
répondu LamdaComplex 2017-04-30 00:53:02

il y a plusieurs méthodes que vous pouvez utiliser qui existent dans l'espace de nom System.IO :

Directory.GetLogicalDrives() // Returns an array of strings like "c:\"
Path.GetInvalidFileNameChars() // Returns an array of characters that cannot be used in a file name
Path.GetInvalidPathChars() // Returns an array of characters that cannot be used in a path.

comme suggéré vous pourriez alors faire ceci:

bool IsValidFilename(string testName) {
    string regexString = "[" + Regex.Escape(Path.GetInvalidPathChars()) + "]";
    Regex containsABadCharacter = new Regex(regexString);
    if (containsABadCharacter.IsMatch(testName)) {
        return false;
    }

    // Check for drive
    string pathRoot = Path.GetPathRoot(testName);
    if (Directory.GetLogicalDrives().Contains(pathRoot)) {
        // etc
    }

    // other checks for UNC, drive-path format, etc

    return true;
}
8
répondu Micah 2017-04-30 00:52:39

Même si le fichier est valide, vous pouvez toujours touch pour être sur que l'utilisateur a l'autorisation d'écrire.

si vous ne voulez pas écraser le disque avec des centaines de fichiers en peu de temps, je pense que créer un fichier vide est une approche raisonnable.

si vous voulez vraiment quelque chose de plus léger, comme simplement vérifier pour les caractères invalides, puis comparer votre nom de fichier contre le chemin.GetInvalidFileNameChars ().

5
répondu Michael Haren 2009-01-07 21:12:20

pensait que je posterais une solution que j'ai concoctée à partir de morceaux de réponses que j'ai trouvées après avoir cherché une solution robuste au même problème. Espérons que cela aide quelqu'un d'autre.

using System;
using System.IO;
//..

public static bool ValidateFilePath(string path, bool RequireDirectory, bool IncludeFileName, bool RequireFileName = false)
{
    if (string.IsNullOrEmpty(path)) { return false; }
    string root = null;
    string directory = null;
    string filename = null;
    try
    {
        // throw ArgumentException - The path parameter contains invalid characters, is empty, or contains only white spaces.
        root = Path.GetPathRoot(path);

        // throw ArgumentException - path contains one or more of the invalid characters defined in GetInvalidPathChars.
        // -or- String.Empty was passed to path.
        directory = Path.GetDirectoryName(path);

        // path contains one or more of the invalid characters defined in GetInvalidPathChars
        if (IncludeFileName) { filename = Path.GetFileName(path); }
    }
    catch (ArgumentException)
    {
        return false;
    }

    // null if path is null, or an empty string if path does not contain root directory information
    if (String.IsNullOrEmpty(root)) { return false; }

    // null if path denotes a root directory or is null. Returns String.Empty if path does not contain directory information
    if (String.IsNullOrEmpty(directory)) { return false; }

    if (RequireFileName)
    {
        // if the last character of path is a directory or volume separator character, this method returns String.Empty
        if (String.IsNullOrEmpty(filename)) { return false; }

        // check for illegal chars in filename
        if (filename.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) { return false; }
    }
    return true;
}
3
répondu Enisle 2018-09-17 23:13:04

Plusieurs du système.IO.Les méthodes Path vont lancer des exceptions si le chemin ou le nom de fichier n'est pas valide:

  • Chemin d'accès.IsPathRooted ()
  • Chemin d'accès.GetFileName ()

http://msdn.microsoft.com/en-us/library/system.io.path_methods.aspx

2
répondu shackett 2009-01-07 21:16:34

utilisez la GetInvalidFileNameChars méthode sur le Path classe dans le System.IO namespace pour déterminer quels caractères sont illégaux dans un nom de fichier.

pour le faire dans un chemin, appeler la statique GetInvalidPathChars méthode sur la même classe.

pour déterminer si la racine d'un chemin est valide, vous appelleriez la statique GetPathRoot méthode sur la classe Path pour obtenir la racine, puis utiliser la Directory classe pour déterminer si elle est valide. Ensuite, vous pouvez valider le reste du chemin normalement.

2
répondu casperOne 2011-06-10 16:45:28

vous obtiendrez les disques durs de la machine:

System.IO.DriveInfo.GetDrives()

ces deux méthodes vous permettront de vérifier les mauvais caractères:

System.IO.Path.GetInvalidFileNameChars();
System.IO.Path.GetInvalidPathChars();
1
répondu Austin Salonen 2009-01-07 21:24:04

j'ai eu de la chance en utilisant des expressions régulières comme d'autres l'ont montré.

une chose à garder à l'esprit est que Windows au moins interdit certains noms de fichiers qui contiennent des caractères légaux. Quelques-uns me viennent à l'esprit: com, nul, prn.

Je ne l'ai pas avec moi maintenant, mais j'ai un regex qui prend ces noms de fichier en considération. Si vous voulez je peux le poster, sinon je suis sûr que vous pouvez le trouver de la même façon que je l'ai fait: Google.

- Jay

1
répondu Jay Riggs 2009-01-07 21:38:04

Je ne sais rien de la boîte qui peut juste valider tout cela pour vous, cependant la classe Path dans .NET peut vous aider énormément.

pour commencer, il a:

char[] invalidChars = Path.GetInvalidFileNameChars(); //returns invalid charachters

ou:

Path.GetPathRoot(string); // will return the root.
1
répondu BFree 2017-04-30 00:53:31

essayer cette méthode qui essaierait de couvrir pour tous les scénarios D'Exceptions possibles. Cela fonctionnerait pour presque tous les chemins liés à Windows.

/// <summary>
/// Validate the Path. If path is relative append the path to the project directory by default.
/// </summary>
/// <param name="path">Path to validate</param>
/// <param name="RelativePath">Relative path</param>
/// <param name="Extension">If want to check for File Path</param>
/// <returns></returns>
private static bool ValidateDllPath(ref string path, string RelativePath = "", string Extension = "") {
    // Check if it contains any Invalid Characters.
    if (path.IndexOfAny(Path.GetInvalidPathChars()) == -1) {
        try {
            // If path is relative take %IGXLROOT% as the base directory
            if (!Path.IsPathRooted(path)) {
                if (string.IsNullOrEmpty(RelativePath)) {
                    // Exceptions handled by Path.GetFullPath
                    // ArgumentException path is a zero-length string, contains only white space, or contains one or more of the invalid characters defined in GetInvalidPathChars. -or- The system could not retrieve the absolute path.
                    // 
                    // SecurityException The caller does not have the required permissions.
                    // 
                    // ArgumentNullException path is null.
                    // 
                    // NotSupportedException path contains a colon (":") that is not part of a volume identifier (for example, "c:\"). 
                    // PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

                    // RelativePath is not passed so we would take the project path 
                    path = Path.GetFullPath(RelativePath);

                } else {
                    // Make sure the path is relative to the RelativePath and not our project directory
                    path = Path.Combine(RelativePath, path);
                }
            }

            // Exceptions from FileInfo Constructor:
            //   System.ArgumentNullException:
            //     fileName is null.
            //
            //   System.Security.SecurityException:
            //     The caller does not have the required permission.
            //
            //   System.ArgumentException:
            //     The file name is empty, contains only white spaces, or contains invalid characters.
            //
            //   System.IO.PathTooLongException:
            //     The specified path, file name, or both exceed the system-defined maximum
            //     length. For example, on Windows-based platforms, paths must be less than
            //     248 characters, and file names must be less than 260 characters.
            //
            //   System.NotSupportedException:
            //     fileName contains a colon (:) in the middle of the string.
            FileInfo fileInfo = new FileInfo(path);

            // Exceptions using FileInfo.Length:
            //   System.IO.IOException:
            //     System.IO.FileSystemInfo.Refresh() cannot update the state of the file or
            //     directory.
            //
            //   System.IO.FileNotFoundException:
            //     The file does not exist.-or- The Length property is called for a directory.
            bool throwEx = fileInfo.Length == -1;

            // Exceptions using FileInfo.IsReadOnly:
            //   System.UnauthorizedAccessException:
            //     Access to fileName is denied.
            //     The file described by the current System.IO.FileInfo object is read-only.-or-
            //     This operation is not supported on the current platform.-or- The caller does
            //     not have the required permission.
            throwEx = fileInfo.IsReadOnly;

            if (!string.IsNullOrEmpty(Extension)) {
                // Validate the Extension of the file.
                if (Path.GetExtension(path).Equals(Extension, StringComparison.InvariantCultureIgnoreCase)) {
                    // Trim the Library Path
                    path = path.Trim();
                    return true;
                } else {
                    return false;
                }
            } else {
                return true;

            }
        } catch (ArgumentNullException) {
            //   System.ArgumentNullException:
            //     fileName is null.
        } catch (System.Security.SecurityException) {
            //   System.Security.SecurityException:
            //     The caller does not have the required permission.
        } catch (ArgumentException) {
            //   System.ArgumentException:
            //     The file name is empty, contains only white spaces, or contains invalid characters.
        } catch (UnauthorizedAccessException) {
            //   System.UnauthorizedAccessException:
            //     Access to fileName is denied.
        } catch (PathTooLongException) {
            //   System.IO.PathTooLongException:
            //     The specified path, file name, or both exceed the system-defined maximum
            //     length. For example, on Windows-based platforms, paths must be less than
            //     248 characters, and file names must be less than 260 characters.
        } catch (NotSupportedException) {
            //   System.NotSupportedException:
            //     fileName contains a colon (:) in the middle of the string.
        } catch (FileNotFoundException) {
            // System.FileNotFoundException
            //  The exception that is thrown when an attempt to access a file that does not
            //  exist on disk fails.
        } catch (IOException) {
            //   System.IO.IOException:
            //     An I/O error occurred while opening the file.
        } catch (Exception) {
            // Unknown Exception. Might be due to wrong case or nulll checks.
        }
    } else {
        // Path contains invalid characters
    }
    return false;
}
1
répondu vCillusion 2017-04-30 00:56:13

probablement la dernière méthode consiste à construire une méthode personnalisée mélangeant une combinaison de regex et un petit coup d'oeil vers le haut sur votre système de fichiers (pour voir les lecteurs, par exemple)

0
répondu tanathos 2009-01-07 21:13:32

Pense qu'il est trop tard pour répondre mais... :) en cas de chemin avec un nom de volume, vous pouvez écrire quelque chose comme ceci:

using System;
using System.Linq;
using System.IO;

// ...

var drives = Environment.GetLogicalDrives();
var invalidChars = Regex.Replace(new string(Path.GetInvalidFileNameChars()), "[\\/]", "");
var drive = drives.FirstOrDefault(d => filePath.StartsWith(d));
if (drive != null) {
    var fileDirPath = filePath.Substring(drive.Length);
    if (0 < fileDirPath.Length) {
        if (fileDirPath.IndexOfAny(invalidChars.ToCharArray()) == -1) {
            if (Path.Combine(drive, fileDirPath) != drive) {
                // path correct and we can proceed
            }
        }
    }
}
0
répondu QgecNick 2017-04-30 00:54:52