文章自动生成

使用截取字符串的方法,打乱原有文档的顺序,重新组合。截取字符串时只考虑了“,”间隔,没有考虑其他标点符号,也没有考虑重组的可读性。留待以后改善!

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

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

        public string txt;//指定路径下的文本内容
        public string subtxt;//截取到的文本内容
        public int index;//截取的索引
        public int Frist=0;//判断是否从文本开始截取
        public int a;//改变“,”的索引位置
        private void button1_Click(object sender, EventArgs e)
        {
            //openFileDialog1.ShowDialog();
            openFileDialog1.Filter = "文本文件|*.txt";
            DialogResult isok = openFileDialog1.ShowDialog();//枚举用户对对话框进行的操作
            if (isok==DialogResult.OK)
            {               
                string s = openFileDialog1.FileName;//选定的文件路径
                if (listBox1.Items.Contains(s))
                {
                    MessageBox.Show("重复的选项!");
                }
                else
                {
                    listBox1.Items.Add(s);//添加路径
                }
            }
            
        }

        public void subs()
        {
            ArrayList al = new ArrayList();
            for (int i = 0; i < listBox1.Items.Count; i++)//选定的文本个数
            {
                string s = listBox1.Items[i].ToString();
                StreamReader sr = new StreamReader(s, Encoding.Default);//读取文本内容
                txt = sr.ReadToEnd();
                txt.Trim();//去掉首尾空格
                index = txt.IndexOf("");
                if (index != -1)//为了判断文本中是否有“,”
                {
                    while (true)
                    {
                        if (Frist == 0)
                        {
                            subtxt = txt.Substring(0,index);//截取字符
                            a = index + 1;//索引加1,略过这个标点,从下一个开始查
                            Frist += 1;
                            al.Add(subtxt);
                        }
                        else
                        {
                            index = txt.IndexOf("", a);
                            subtxt = txt.Substring(a, index-a);
                            a = index + 1;
                            al.Add(subtxt);
                            if (a > txt.LastIndexOf(""))
                            {                               
                                a = 0;
                                break;
                            }
                        }
                    }
                }
                sr.Close();
            }
            Frist = 0;
            string FileName = @"D:生成文本.txt";//创建一个新的txt文件,来存储打乱的文本
            if (!File.Exists(FileName))//判断是否已经创建了此文本
            {
                File.Create(FileName).Close();
            }
            else
            {
                File.CreateText(FileName).Close();//创建文本文件
            }
            Random r = new Random();//取随机数,来随机组合字符串
           
            for (int i = 0; i < al.Count; i++)
            {
                int suijishu = r.Next(0, al.Count);
                StreamReader sre = new StreamReader(FileName, Encoding.Default);
                if (sre.ReadToEnd()=="")
                {
                    sre.Close();
                    StreamWriter sw = new StreamWriter(FileName, false);//写入流,写入字符串
                    sw.Write(al[suijishu]);
                    sw.Close();
                }
                else
                {
                    string jieshou = sre.ReadToEnd();
                    sre.Close();
                    StreamWriter sw = new StreamWriter(FileName, true);
                    sw.Write(jieshou+al[suijishu]);
                    sw.Flush();
                    sw.Close();
                }
                sre.Close();
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            subs();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult isok = openFileDialog1.ShowDialog();
            if (isok == DialogResult.OK)
            {
                string s = openFileDialog1.FileName;
                System.Diagnostics.Process.Start(s);//打开指定路径下的文件
            }

        }
    }
}
原文地址:https://www.cnblogs.com/happinesshappy/p/4574319.html