• 大小: 67KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-10
  • 语言: C#
  • 标签: C#  TXT读写  

资源简介

C#操作TXT的实力,包括新建,删除,以及字符串处理

资源截图

代码片段和文件信息

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;

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

        private void button1_Click(object sender EventArgs e)
        {

            textBox1.Text =“C:\\Users\\jack\\Desktop\\cass4.0数据格式\\“;
            textBox2.Text =“G:\\txt处理后\\“;

            
            string SourcePath=textBox1.Text;
            string DestinationPath = textBox2.Text;

            DirectoryInfo Directory = new DirectoryInfo(SourcePath);

            foreach (FileInfo file in Directory.GetFiles())
            {   
             
            StreamReader sr = new StreamReader(SourcePath + file.Name);
            string[] lines = File.ReadAllLines(SourcePath + file.Name);
            int a = 0;
            string Combina=““;

            List needs = new List();
            foreach(string line in lines)
            {
               // a=a+1;
               // string Combina=““;
                if (line.Length >4)
                {
                    needs.Add(line); 
                }     
                   
            }
            needs.RemoveAt(0);
            int count = 0;
            List Combination = new List();
            for (int y = 2; y <= needs.Count; y=y+2)
            {
                Combina =“355“+needs[y-1] +““ +“35“ + needs[y];    //合并
                count = count + 1;          //用来统计合并了多少个数据
                Combination.Add(Combina);       //用另一个数组存储合并后数据
            }

            for (int z = 1; z <= count*2; z++)
            {
                needs.RemoveAt(1);
            }

            for (int i = 0; i < Combination.Count; i++)
            {
                needs.Add(Combination[i]);
            }
            
            WriteInTxt(DestinationPath + file.Name needs);
             
                }


        }


        public static void WriteInTxt(string DestinationPathList needs)
        {
            string WriteInPath = DestinationPath;
            int NumberCount =needs.Count;
            int i=0;

            if (!File.Exists(WriteInPath))
            {
                FileStream fs1 = new FileStream(WriteInPath FileMode.Create FileAccess.Write);//创建写入文件        
                StreamWriter sw = new StreamWriter(fs1);
                for(i = 0;i < NumberCount ; i++)
                {

                    sw.WriteLine(needs[i]);//开始写入值  
                }
                    
                sw.Close();
                fs1.Close();
            }
            else
            {
                FileStream fs = new FileStream(WriteInPath FileMode.Open FileAccess.Write);
                Str

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       9728  2014-10-24 09:47  txt读写\WindowsFormsApplication6\bin\Debug\WindowsFormsApplication6.exe

     文件      30208  2014-10-24 09:47  txt读写\WindowsFormsApplication6\bin\Debug\WindowsFormsApplication6.pdb

     文件      24224  2014-11-17 11:11  txt读写\WindowsFormsApplication6\bin\Debug\WindowsFormsApplication6.vshost.exe

     文件        490  2013-06-18 20:28  txt读写\WindowsFormsApplication6\bin\Debug\WindowsFormsApplication6.vshost.exe.manifest

     文件       3384  2014-10-24 09:47  txt读写\WindowsFormsApplication6\Form1.cs

     文件       2983  2014-10-22 16:14  txt读写\WindowsFormsApplication6\Form1.Designer.cs

     文件       5817  2014-10-22 16:14  txt读写\WindowsFormsApplication6\Form1.resx

     文件       5420  2014-10-22 16:35  txt读写\WindowsFormsApplication6\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7168  2014-11-17 11:11  txt读写\WindowsFormsApplication6\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       1258  2014-10-22 19:05  txt读写\WindowsFormsApplication6\obj\x86\Debug\WindowsFormsApplication6.csproj.FileListAbsolute.txt

     文件        975  2014-10-22 16:14  txt读写\WindowsFormsApplication6\obj\x86\Debug\WindowsFormsApplication6.csproj.GenerateResource.Cache

     文件       9174  2014-10-22 17:02  txt读写\WindowsFormsApplication6\obj\x86\Debug\WindowsFormsApplication6.csprojResolveAssemblyReference.cache

     文件       9728  2014-10-24 09:47  txt读写\WindowsFormsApplication6\obj\x86\Debug\WindowsFormsApplication6.exe

     文件        180  2014-10-22 16:14  txt读写\WindowsFormsApplication6\obj\x86\Debug\WindowsFormsApplication6.Form1.resources

     文件      30208  2014-10-24 09:47  txt读写\WindowsFormsApplication6\obj\x86\Debug\WindowsFormsApplication6.pdb

     文件        180  2014-10-22 16:14  txt读写\WindowsFormsApplication6\obj\x86\Debug\WindowsFormsApplication6.Properties.Resources.resources

     文件       1508  2014-11-17 11:11  txt读写\WindowsFormsApplication6\obj\x86\Debug\数据格式转化.csproj.FileListAbsolute.txt

     文件        975  2014-10-24 09:47  txt读写\WindowsFormsApplication6\obj\x86\Debug\数据格式转化.csproj.GenerateResource.Cache

     文件       9214  2014-10-24 09:47  txt读写\WindowsFormsApplication6\obj\x86\Debug\数据格式转化.csprojResolveAssemblyReference.cache

     文件        505  2014-10-22 15:49  txt读写\WindowsFormsApplication6\Program.cs

     文件       1380  2014-10-22 15:49  txt读写\WindowsFormsApplication6\Properties\AssemblyInfo.cs

     文件       2900  2014-10-22 15:49  txt读写\WindowsFormsApplication6\Properties\Resources.Designer.cs

     文件       5612  2014-10-22 15:49  txt读写\WindowsFormsApplication6\Properties\Resources.resx

     文件       1111  2014-10-22 15:49  txt读写\WindowsFormsApplication6\Properties\Settings.Designer.cs

     文件        249  2014-10-22 15:49  txt读写\WindowsFormsApplication6\Properties\Settings.settings

     文件       3707  2014-10-22 16:14  txt读写\WindowsFormsApplication6\数据格式转化.csproj

     文件        902  2014-10-22 19:07  txt读写\数据格式转化.sln

    ..A..H.     31232  2014-11-07 10:15  txt读写\数据格式转化.suo

    ..A..H.     53248  2014-11-17 11:59  txt读写\数据格式转化.v12.suo

     目录          0  2014-10-22 15:49  txt读写\WindowsFormsApplication6\obj\x86\Debug\TempPE

............此处省略11个文件信息

评论

共有 条评论