Regular expressions in C# for file name validation

后端 未结 4 2017
小鲜肉
小鲜肉 2020-12-29 06:17

What is a good regular expression that can validate a text string to make sure it is a valid Windows filename? (AKA not have \\/:*?\"<>| characters).

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-29 06:56

    As answered already, GetInvalidFileNameChars should do it for you, and you don't even need the overhead of regular expressions:

    if (proposedFilename.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
    {
      MessageBox.Show("The filename is invalid");
      return;
    }
    

提交回复
热议问题