本文介绍了从文件名(或目录,文件夹,文件)删除无效的(不允许的,坏的)人物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经写了这个小方法来实现的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();
}
这篇关于从文件名(或目录,文件夹,文件)删除无效的(不允许的,坏的)人物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!