namespace txtFilter
{
partial class frmFilter
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.cmdFilter = new System.Windows.Forms.Button();
this.cmdOpenPath = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.openDialog = new System.Windows.Forms.OpenFileDialog();
this.saveDialog = new System.Windows.Forms.SaveFileDialog();
this.listView1 = new System.Windows.Forms.ListView();
this.path = new System.Windows.Forms.ColumnHeader();
this.result = new System.Windows.Forms.ColumnHeader();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// cmdFilter
//
this.cmdFilter.Location = new System.Drawing.Point(297, 135);
this.cmdFilter.Name = "cmdFilter";
this.cmdFilter.Size = new System.Drawing.Size(100, 23);
this.cmdFilter.TabIndex = 0;
this.cmdFilter.Text = "转换";
this.cmdFilter.UseVisualStyleBackColor = true;
this.cmdFilter.Click += new System.EventHandler(this.cmdFilter_Click);
//
// cmdOpenPath
//
this.cmdOpenPath.Location = new System.Drawing.Point(21, 3);
this.cmdOpenPath.Name = "cmdOpenPath";
this.cmdOpenPath.Size = new System.Drawing.Size(100, 23);
this.cmdOpenPath.TabIndex = 2;
this.cmdOpenPath.Text = "选择文件";
this.cmdOpenPath.UseVisualStyleBackColor = true;
this.cmdOpenPath.Click += new System.EventHandler(this.cmdOpenPath_Click);
//
// panel1
//
this.panel1.Controls.Add(this.listView1);
this.panel1.Controls.Add(this.cmdFilter);
this.panel1.Controls.Add(this.cmdOpenPath);
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(417, 173);
this.panel1.TabIndex = 5;
//
// openDialog
//
this.openDialog.FileName = "openFileDialog1";
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.path,
this.result});
this.listView1.Location = new System.Drawing.Point(21, 32);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(376, 97);
this.listView1.TabIndex = 7;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// path
//
this.path.Text = "文件路径";
this.path.Width = 300;
//
// result
//
this.result.Text = "执行结果";
//
// frmFilter
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.ClientSize = new System.Drawing.Size(444, 198);
this.Controls.Add(this.panel1);
this.MaximizeBox = false;
this.Name = "frmFilter";
this.Text = "TXT转换";
this.Load += new System.EventHandler(this.frmFilter_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button cmdFilter;
private System.Windows.Forms.Button cmdOpenPath;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.OpenFileDialog openDialog;
private System.Windows.Forms.SaveFileDialog saveDialog;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader path;
private System.Windows.Forms.ColumnHeader result;
}
}
--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace txtFilter
{
public partial class frmFilter : Form
{
public frmFilter()
{
InitializeComponent();
}
private void frmFilter_Load(object sender, EventArgs e)
{
}
private void cmdFilter_Click(object sender, EventArgs e)
{
if (openDialog.FileNames.Length > 0)
{
FilterMethod();
}
else
{
MessageBox.Show("请选择需要转换的文本!");
}
}
private void cmdOpenPath_Click(object sender, EventArgs e)
{
OpenDialog_Method();
}
public void FilterMethod()
{
try
{
DateTime dtBegin = System.DateTime.Now;
this.Cursor = Cursors.WaitCursor;
listView1.Items.Clear();
for (int i = 0; i < openDialog.FileNames.Length; i++)
{
if (filter.ResumeTxt(openDialog.FileNames[i].ToString()) == true)
{
ListViewItem lv = new ListViewItem();
lv.Text = openDialog.FileNames[i];
lv.SubItems.Add("执行成功");
listView1.Items.Add(lv);
}
else
{
ListViewItem lv = new ListViewItem();
lv.Text = openDialog.FileNames[i];
lv.SubItems.Add("执行失败");
listView1.Items.Add(lv);
}
}
this.Cursor = Cursors.Arrow;
DateTime dtEnd = System.DateTime.Now;
System.TimeSpan ts = dtEnd - dtBegin;
string spendTime = ts.TotalSeconds.ToString() + "秒";
MessageBox.Show(spendTime);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void OpenDialog_Method()
{
try
{
//支持多选
openDialog.Multiselect = true;
//只显示.txt文件
openDialog.Filter = "txt files(*.txt)|*.txt";
openDialog.FilterIndex = 2;
if (openDialog.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < openDialog.FileNames.Length; i++)
{
ListViewItem lv = new ListViewItem();
lv.Text = openDialog.FileNames[i];
listView1.Items.Add(lv);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
----------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace txtFilter
{
public class filter
{
public filter()
{
}
public static bool ResumeTxt(string openPath)
{
bool state = false;
//获取文件名
string fileName = System.IO.Path.GetFileNameWithoutExtension(openPath);
//指定保存路径和保存名称
string savePath = Application.StartupPath + "//"+fileName+"_Done.txt";
string pathSrc = openPath;//原文件路径
string pathDes = savePath;//目标文件路径
StreamReader sr = new StreamReader(pathSrc, Encoding.Default);//读取
StreamWriter sw = new StreamWriter(pathDes, false, Encoding.Default);
string temp;//临时字符串
StringBuilder sb = new StringBuilder();//缓冲
const int COUNT = 1000000;//每次处理字符串长度最少1百万(大约在1M~2M字节之间)
while((temp = sr.ReadLine()) != null)//每次读一行,如果读出的字符串不为空
{
//在这里对temp进行处理
temp = ValiFilter.FilterPhoneAndEncrypt(temp);
//处理完了
sb.Append(temp);
sb.Append(Environment.NewLine);//增加换行符
if(sb.Length >= COUNT)
{
sw.Write(sb.ToString());
sb.Length = 0;//缓冲置零
}
}
if(sb.Length > 0)//处理最后不足限制的字节
{
sw.Write(sb.ToString());
sb.Length = 0;//缓冲置零
}
sr.Close();//释放资源
sw.Close();//释放资源
state = true;
return state;
}
}
}