码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2014-07-09 17:40:26      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:winform   文件   os   art   cti   for   

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

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

标签:winform   文件   os   art   cti   for   

原文地址:http://www.cnblogs.com/liziqiang/p/3831910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!