代码之家  ›  专栏  ›  技术社区  ›  Game_Overture

C中用于文件名验证的正则表达式

  •  19
  • Game_Overture  · 技术社区  · 16 年前

    什么是好的正则表达式,可以验证文本字符串以确保它是有效的Windows文件名?(又说没有 \/:*?"<>| 字符)。

    我想用它如下:

    // Return true if string is invalid.
    if (Regex.IsMatch(szFileName, "<your regex string>"))
    {
        // Tell user to reformat their filename.
    }
    
    4 回复  |  直到 16 年前
        1
  •  48
  •   Isak Savo    16 年前

    if (proposedFilename.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
    {
      MessageBox.Show("The filename is invalid");
      return;
    }
    
        3
  •  0
  •   Drejc    16 年前

        4
  •  0
  •   Viacheslav Ivanov    16 年前

    if(@"C:\A.txt".IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
    {
      MessageBox.Show("The filename is invalid");
      return;
    }