我需要从文件中去除特殊字符,我尝试了以下基于这个例子的代码,但它会产生一些错误。我需要这段代码适用于基于asp.net webform的应用程序。
using System;
using System.Linq;
using System.Text.RegularExpressions;
public class Test {
public static void Main() {
// your code goes here
var file_name = GetValidFileName("this is)file<ame.txt");
Console.WriteLine(file_name);
private static string GetValidFileName(string fileName) {
// remove any invalid character from the filename.
return Regex.Replace(fileName.Trim(), "[^A-Za-z0-9_. ]+", "");
}
}
}
在ideone.com上,有与输出相关的示例代码。