zoukankan      html  css  js  c++  java
  • Winform读取文档。然后创建,奇数行保存一个文档,偶数行保存一个文档

    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();
    }
    }
    }
    }

    }
    }

  • 相关阅读:
    微信支付的安全漏洞之XXE
    IP地址分类(A类 B类 C类 D类 E类)
    MySql新增表的字段,删除表字段
    Java基础之中间件的初识
    Java基础之IO框架
    微信H5支付坑一--手续费未结算
    设计模式之简单工厂模式
    nginx负载均衡的5种策略(转载)
    Mybatis注意点之#与$区别
    RSF 分布式 RPC 服务框架的分层设计
  • 原文地址:https://www.cnblogs.com/liziqiang/p/3831910.html
Copyright © 2011-2022 走看看