日期:2014-05-18 浏览次数:20532 次
public void TextImport(string path, string name) { FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs.Name, System.Text.Encoding.GetEncoding("GB2312"), true); sr.BaseStream.Seek(0, SeekOrigin.Begin); Regex reg = new Regex(@"1[3|5|8]\d{9}"); while (!sr.EndOfStream) { if (!reg.IsMatch(sr.ReadLine()))//插入电话号码 .......................... } sr.Close();//释放资源 fs.Close();//关闭流 Response.Write("<script>alert('导入成功!')</script>"); }
------解决方案--------------------
//获得readTxt文件下所有 .txt文件 循环读取 string[] fileList = Directory.GetFiles(@"d:\readTxt", "*.txt"); foreach (string strPath in fileList) { using (StreamReader read = new StreamReader(strPath)) { string str = read.ReadLine();//逐行读取 插入数据库 } }