Winform读取文档。然后创建,奇数行保存一个文档,偶数行保存一个文档

时间:2014-07-09 17:40:26   收藏:0   阅读:264

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace TextModify
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button3_Click(object sender, EventArgs e)
{
Product("CN");
Product("EN");
MessageBox.Show("生成文件成功");
}

private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog bro = new FolderBrowserDialog();
bro.ShowDialog();
txtPath.Text = bro.SelectedPath;
}

private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog bro = new FolderBrowserDialog();
bro.ShowDialog();
txtSavePath.Text = bro.SelectedPath;
}

private void Product(string Name)
{
string[] filenames = Directory.GetFiles(txtPath.Text);
foreach (string files in filenames)
{
string strExteenstion = Path.GetExtension(files);
string[] str = new string[] { ".txt" };
string newPath = txtSavePath.Text + "\\" + Name;
Directory.CreateDirectory(newPath);
if (str.Contains(strExteenstion))
{
string oldName = Path.GetFileName(files);
string newCNName = Name + oldName;
File.Create(newPath + "\\" + newCNName).Close();//创建文件夹
StreamReader srCN = new StreamReader(files, Encoding.Default);
FileStream fsCN = new FileStream(newPath + "\\" + newCNName, FileMode.Append);
StreamWriter swCN = new StreamWriter(fsCN);
try
{
int i = 1;
string s = srCN.ReadLine();
while (s != null)
{
if (i % 2 == 0)
{
i++;
if (Name == "CN")
{
swCN.WriteLine(s);
}
}
else
{

i++;
if (Name == "EN")
{
swCN.WriteLine(s);
}

}
s = srCN.ReadLine();
}
}
catch (Exception ex)
{
MessageBox.Show("操作失败,失败原因:" + ex.Message);
}

finally
{
srCN.Close();
swCN.Close();
fsCN.Close();
}
}
}
}

}
}

Winform读取文档。然后创建,奇数行保存一个文档,偶数行保存一个文档,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!