有时候从网上扒视频的时候,需要把视频标题也拔下来,以标题命名视频名字。
往往会出现非法字符,存不了。
.net 提供了一个好的方法。。
public static char[] GetInvalidFileNameChars ();
获取包含不允许在文件名中使用的字符的数组。
直接遍历下取到的标题,把非法字符给替换为空就行了。
foreach (var c in Path.GetInvalidFileNameChars())
{
desc= desc.Replace(c.ToString(), string.Empty);
}
public string GetInvalidFileName(string str)
{
foreach (var c in Path.GetInvalidFileNameChars())
{
str = str.Replace(c.ToString(), " ");//这里可以自定义
}
return str;
}