首页  编辑  

让文件名合法

Tags: /C#/文件目录操作/   Date Created:
把字符串变为合法的文件名:
private static string MakeValidFileName(string name)
{
	string invalidChars = System.Text.RegularExpressions.Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars()));
	string invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars);

	return System.Text.RegularExpressions.Regex.Replace(name, invalidRegStr, "_");
}