Method to Judge Illegal Characters of File Name or File Path in C

  • 2021-06-28 13:41:05
  • OfStack

Illegal character judgment in file path or save template


1) judgment not empty


string strTemplateName = txtTemplateName.Text;
      if (string.IsNullOrWhiteSpace(strTemplateName))
      {
        Show(" Please enter the template name! ", " Tips ", .Information, OK);
        txtTemplateName.Focus();
        return;
      }

2) Then make illegal character judgment for strTemplateName


if (strTemplateName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
      {
        // Contains illegal characters  \ / : * ? " < > |  etc. 
        Show(" Template name contains illegal characters, please re-enter ", " error ", Error, OK);
        txtTemplateName.Focus();
        return;
      }


3) path refers to the io dynamic library of the system.


Related articles: