从文件名(或目录,文件夹,文件)删除无效的(不允许的,坏的)人物不允许、文件名、文件夹、人物

由网友(Laity (俗人))分享简介:我已经写了这个小方法来实现的SUBJ的目标。但是,有这样做的更有效的(简单)的方式?我希望这可以帮助别人谁将会搜索这个像我一样。I've wrote this little method to achieve the goal in the subj., however, is there more efficien...

我已经写了这个小方法来实现的SUBJ的目标。但是,有这样做的更有效的(简单)的方式?我希望这可以帮助别人谁将会搜索这个像我一样。

I've wrote this little method to achieve the goal in the subj., however, is there more efficient (simpler) way of doing this? I hope this can help somebody who will search for this like I did.

var fileName = new System.Text.StringBuilder();
fileName.Append("*Bad/ :, Filename,? ");
// get rid of invalid chars
while (fileName.ToString().IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) > -1)
{
    fileName = fileName.Remove(fileName.ToString().IndexOfAny(System.IO.Path.GetInvalidFileNameChars()), 1);
}

推荐答案

请尝试以下

public string MakeValidFileName(string name) {
  var builder = new StringBuilder();
  var invalid = System.IO.Path.GetInvalidFileNameChars();
  foreach ( var cur in name ) {
    if ( !invalid.Contains(cur) ) {
      builder.Append(cur);
    }
  }
  return builder.ToString();
}
阅读全文

相关推荐

最新文章