.Net: Как проверить наличие недопустимых символов в пути? - PullRequest
26 голосов
/ 13 марта 2010

Есть ли способ проверить, есть ли в строке, предназначенной для пути, недопустимые символы, в .Net? Я знаю, что могу перебрать каждый символ в Path.InvalidPathChars, чтобы увидеть, содержит ли он мой String, но я бы предпочел простое, возможно, более формальное решение.

Есть ли один?

Я обнаружил, что все еще получаю исключение, если проверяю только Get

Обновление:

Я обнаружил, что GetInvalidPathChars не охватывает каждый недопустимый символ пути. GetInvalidFileNameChars имеет еще 5, включая «?», С которым я сталкивался. Я собираюсь переключиться на это и сообщу, если это тоже окажется неадекватным.

Обновление 2:

GetInvalidFileNameChars определенно не то, что я хочу. Он содержит ':', который будет содержать любой абсолютный путь ("C: \ what"). Я думаю, что мне все-таки придется использовать GetInvalidPathChars и добавить '?' и любые другие персонажи, которые вызывают у меня проблемы по мере их появления. Лучшие решения приветствуются.

Ответы [ 6 ]

40 голосов
/ 13 марта 2010

InvalidPathChars устарела. Вместо этого используйте GetInvalidPathChars ():

    public static bool FilePathHasInvalidChars(string path)
    {

        return (!string.IsNullOrEmpty(path) && path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0);
    }

Редактировать: немного длиннее, но обрабатывает путь и недопустимые символы файла в одной функции:

    // WARNING: Not tested
    public static bool FilePathHasInvalidChars(string path)
    {
        bool ret = false;
        if(!string.IsNullOrEmpty(path))
        {
            try
            {
                // Careful!
                //    Path.GetDirectoryName("C:\Directory\SubDirectory")
                //    returns "C:\Directory", which may not be what you want in
                //    this case. You may need to explicitly add a trailing \
                //    if path is a directory and not a file path. As written, 
                //    this function just assumes path is a file path.
                string fileName = System.IO.Path.GetFileName(path);
                string fileDirectory = System.IO.Path.GetDirectoryName(path);

                // we don't need to do anything else,
                                    // if we got here without throwing an 
                                    // exception, then the path does not
                                    // contain invalid characters
            }
            catch (ArgumentException)
            {
                                    // Path functions will throw this 
                                    // if path contains invalid chars
                ret = true;
            }
        }
        return ret;
    }
4 голосов
/ 16 ноября 2011

Будьте осторожны, полагаясь на Path.GetInvalidFileNameChars, что может быть не так надежно, как вы думаете. Обратите внимание на следующее замечание в документации MSDN по Path.GetInvalidFileNameChars:

Массив, возвращаемый этим методом, не обязательно содержит полный набор символов, недопустимых в именах файлов и каталогов. Полный набор недопустимых символов может варьироваться в зависимости от файловой системы. Например, на настольных платформах под управлением Windows недопустимые символы пути могут включать символы ASCII / Unicode с 1 по 31, а также кавычки ("), меньше чем (<), больше чем (>), pipe (|), backspace ( \ b), ноль (\ 0) и табуляция (\ t).

Не лучше с Path.GetInvalidPathChars методом. Он содержит точно такое же замечание.

2 голосов
/ 09 ноября 2015

Возможно, уже слишком поздно для вас, но может помочь кому-то еще. Я столкнулся с той же проблемой, и мне нужно было найти надежный способ очистки пути.

Вот что я использовал в 3 этапа:

Шаг 1: Пользовательская очистка.

public static string RemoveSpecialCharactersUsingCustomMethod(this string expression, bool removeSpecialLettersHavingASign = true)
{
    var newCharacterWithSpace = " ";
    var newCharacter = "";

    // Return carriage handling
    // ASCII LINE-FEED character (LF),
    expression = expression.Replace("\n", newCharacterWithSpace);
    // ASCII CARRIAGE-RETURN character (CR) 
    expression = expression.Replace("\r", newCharacterWithSpace);

    // less than : used to redirect input, allowed in Unix filenames, see Note 1
    expression = expression.Replace(@"<", newCharacter);
    // greater than : used to redirect output, allowed in Unix filenames, see Note 1
    expression = expression.Replace(@">", newCharacter);
    // colon: used to determine the mount point / drive on Windows; 
    // used to determine the virtual device or physical device such as a drive on AmigaOS, RT-11 and VMS; 
    // used as a pathname separator in classic Mac OS. Doubled after a name on VMS, 
    // indicates the DECnet nodename (equivalent to a NetBIOS (Windows networking) hostname preceded by "\\".). 
    // Colon is also used in Windows to separate an alternative data stream from the main file.
    expression = expression.Replace(@":", newCharacter);
    // quote : used to mark beginning and end of filenames containing spaces in Windows, see Note 1
    expression = expression.Replace(@"""", newCharacter);
    // slash : used as a path name component separator in Unix-like, Windows, and Amiga systems. 
    // (The MS-DOS command.com shell would consume it as a switch character, but Windows itself always accepts it as a separator.[16][vague])
    expression = expression.Replace(@"/", newCharacter);
    // backslash : Also used as a path name component separator in MS-DOS, OS/2 and Windows (where there are few differences between slash and backslash); allowed in Unix filenames, see Note 1
    expression = expression.Replace(@"\", newCharacter);
    // vertical bar or pipe : designates software pipelining in Unix and Windows; allowed in Unix filenames, see Note 1
    expression = expression.Replace(@"|", newCharacter);
    // question mark : used as a wildcard in Unix, Windows and AmigaOS; marks a single character. Allowed in Unix filenames, see Note 1
    expression = expression.Replace(@"?", newCharacter);
    expression = expression.Replace(@"!", newCharacter);
    // asterisk or star : used as a wildcard in Unix, MS-DOS, RT-11, VMS and Windows. Marks any sequence of characters 
    // (Unix, Windows, later versions of MS-DOS) or any sequence of characters in either the basename or extension 
    // (thus "*.*" in early versions of MS-DOS means "all files". Allowed in Unix filenames, see note 1
    expression = expression.Replace(@"*", newCharacter);
    // percent : used as a wildcard in RT-11; marks a single character.
    expression = expression.Replace(@"%", newCharacter);
    // period or dot : allowed but the last occurrence will be interpreted to be the extension separator in VMS, MS-DOS and Windows. 
    // In other OSes, usually considered as part of the filename, and more than one period (full stop) may be allowed. 
    // In Unix, a leading period means the file or folder is normally hidden.
    expression = expression.Replace(@".", newCharacter);
    // space : allowed (apart MS-DOS) but the space is also used as a parameter separator in command line applications. 
    // This can be solved by quoting, but typing quotes around the name every time is inconvenient.
    //expression = expression.Replace(@"%", " ");
    expression = expression.Replace(@"  ", newCharacter);

    if (removeSpecialLettersHavingASign)
    {
        // Because then issues to zip
        // More at : http://www.thesauruslex.com/typo/eng/enghtml.htm
        expression = expression.Replace(@"ê", "e");
        expression = expression.Replace(@"ë", "e");
        expression = expression.Replace(@"ï", "i");
        expression = expression.Replace(@"œ", "oe");
    }

    return expression;
}

Шаг 2. Проверьте все недопустимые символы, которые еще не удалены.

На дополнительном этапе проверки я использую метод Path.GetInvalidPathChars(), опубликованный выше, чтобы обнаружить любые потенциально недопустимые символы, которые еще не удалены.

public static bool ContainsAnyInvalidCharacters(this string path)
{
    return (!string.IsNullOrEmpty(path) && path.IndexOfAny(Path.GetInvalidPathChars()) >= 0);
}

Шаг 3: Очистить все специальные символы, обнаруженные на шаге 2.

И, наконец, я использую этот метод в качестве последнего шага для очистки всего, что осталось. (из Как удалить недопустимые символы из пути и имени файла? ):

public static string RemoveSpecialCharactersUsingFrameworkMethod(this string path)
{
    return Path.GetInvalidFileNameChars().Aggregate(path, (current, c) => current.Replace(c.ToString(), string.Empty));
}

Я регистрирую любой недопустимый символ, не очищенный на первом этапе. Я решил пойти по этому пути, чтобы улучшить свой пользовательский метод, как только обнаружится «утечка». Я не могу полагаться на Path.GetInvalidFileNameChars() из-за следующего утверждения, о котором сообщалось выше (из MSDN):

"Массив, возвращаемый этим методом, не обязательно содержит полный набор символов, которые недопустимы в файле и каталоге имена. «

Возможно, это не идеальное решение, но, учитывая контекст моего приложения и требуемый уровень надежности, это лучшее решение, которое я нашел.

1 голос
/ 04 сентября 2018

По состоянию на .NET 4.7.2 , Path.GetInvalidFileNameChars() сообщает о следующих 41 «плохих» символах.

0x0000    0      '\0'   |    0x000d   13      '\r'   |    0x001b   27  '\u001b'
0x0001    1  '\u0001'   |    0x000e   14  '\u000e'   |    0x001c   28  '\u001c'
0x0002    2  '\u0002'   |    0x000f   15  '\u000f'   |    0x001d   29  '\u001d'
0x0003    3  '\u0003'   |    0x0010   16  '\u0010'   |    0x001e   30  '\u001e'
0x0004    4  '\u0004'   |    0x0011   17  '\u0011'   |    0x001f   31  '\u001f'
0x0005    5  '\u0005'   |    0x0012   18  '\u0012'   |    0x0022   34       '"'
0x0006    6  '\u0006'   |    0x0013   19  '\u0013'   |    0x002a   42       '*'
0x0007    7      '\a'   |    0x0014   20  '\u0014'   |    0x002f   47       '/'
0x0008    8      '\b'   |    0x0015   21  '\u0015'   |    0x003a   58       ':'
0x0009    9      '\t'   |    0x0016   22  '\u0016'   |    0x003c   60       '<'
0x000a   10      '\n'   |    0x0017   23  '\u0017'   |    0x003e   62       '>'
0x000b   11      '\v'   |    0x0018   24  '\u0018'   |    0x003f   63       '?'
0x000c   12      '\f'   |    0x0019   25  '\u0019'   |    0x005c   92      '\\'
                        |    0x001a   26  '\u001a'   |    0x007c  124       '|'

Как отметил * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *. Вы можете использовать следующую строку кода для определения этого точного набора символов:

public static bool IsInvalidFileNameChar(Char c) => c < 64U ?
        (1UL << c & 0xD4008404FFFFFFFFUL) != 0 :
        c == '\\' || c == '|';
1 голос
/ 08 декабря 2015

В итоге я позаимствовал и скомбинировал несколько внутренних реализаций .NET, чтобы придумать эффективный метод:

/// <summary>Determines if the path contains invalid characters.</summary>
/// <remarks>This method is intended to prevent ArgumentException's from being thrown when creating a new FileInfo on a file path with invalid characters.</remarks>
/// <param name="filePath">File path.</param>
/// <returns>True if file path contains invalid characters.</returns>
private static bool ContainsInvalidPathCharacters(string filePath)
{
    for (var i = 0; i < filePath.Length; i++)
    {
        int c = filePath[i];

        if (c == '\"' || c == '<' || c == '>' || c == '|' || c == '*' || c == '?' || c < 32)
            return true;
    }

    return false;
}

Затем я использовал его так, но также обернул в блок try / catch длябезопасность:

if ( !string.IsNullOrWhiteSpace(path) && !ContainsInvalidPathCharacters(path))
{
    FileInfo fileInfo = null;

    try
    {
        fileInfo = new FileInfo(path);
    }
    catch (ArgumentException)
    {            
    }

    ...
}
0 голосов
/ 04 июля 2018

Я тоже опоздал. Но если задача состоит в том, чтобы проверить, ввел ли пользователь что-то допустимое в качестве пути, существует комбинированное решение для путей.

Path.GetInvalidFileNameChars() возвращает список символов, недопустимых для файла, но каталог следует правилам файла, за исключением разделителей (которые мы можем получить из системы) и корневого спецификатора (C:, мы можем просто удалить его из поиска). Да, Path.GetInvalidFileNameChars() возвращает не полный набор, но лучше, чем пытаться найти их все вручную.

Итак:

private static bool CheckInvalidPath(string targetDir)
{
  string root;
  try
  {
    root = Path.GetPathRoot(targetDir);
  }
  catch
  {
    // the path is definitely invalid if it has crashed
    return false;
  }

  // of course it is better to cache it as it creates
  // new array on each call
  char[] chars = Path.GetInvalidFileNameChars();

  // ignore root
  for (int i = root.Length; i < targetDir.Length; i++)
  {
    char c = targetDir[i];

    // separators are allowed
    if (c == Path.DirectorySeparatorChar || c == Path.AltDirectorySeparatorChar)
      continue;

    // check for illegal chars
    for (int j = 0; j < chars.Length; j++)
      if (c == chars[j])
        return false;
  }

  return true;
}

Я обнаружил, что такие методы, как Path.GetFileName, не будут аварийно завершать работу для таких путей, как C:\* (что совершенно неверно), и даже проверки на основе исключений недостаточно. Единственное, что может привести к сбою Path.GetPathRoot, - это недопустимый root (например, CC:\someDir). Так что все остальное нужно делать вручную.

...