在C#.net中合并文件

编程入门 行业动态 更新时间:2024-10-25 20:27:17
本文介绍了在C#中合并文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

朋友们, 如何在C#中附加文件?我在输入文件夹中有一个输入文件夹和4个txt文件.我想合并/附加同一文件夹中的文件,附加的文件将在附加后删除. 文件夹名称:Test 文件名是 1)2012_03_05_05:50:30.txt 2)2012_03_05_05:50:35.txt 3)2012_03_05_05:50:40.txt 4)2012_03_05_05:50:45.txt.在测试文件夹"中有4个文件.我想将文件附加到第一个文件中,然后自动添加.追加或合并文件后应删除第二,第三,第四文件.我该如何实现?

Hi friends, How to append a files in the C#? I have a Input Folder and 4 txt files in the Input Folder. i want to merge/Append a files in the same folder and Appended files will delete after appending . Folder name: Test File Names are 1) 2012_03_05_05:50:30.txt 2) 2012_03_05_05:50:35.txt 3 ) 2012_03_05_05:50:40.txt 4 ) 2012_03_05_05:50:45.txt. here there are 4 files in the Test Folder . I want to append a files into 1st file and Automatically. 2nd,3rd,4th file should be deleted after Appending or merge a files .How can i achieve this?

推荐答案

您可以使用下面的代码,该代码需要4个文件并创建将所有文件的所有数据附加到一个文件后. you can use below code which takes 4 files and create a single file after appending all data of all files. static void Main(string[] args) { string[] Filename = new string[4] {@"F:\Test\2012_03_05_055030.txt", @"F:\Test\2012_03_05_055035.txt", @"F:\Test\2012_03_05_055040.txt", @"F:\Test\2012_03_05_055045.txt"}; string text = System.IO.File.ReadAllText(Filename[0]); int count = 1; while(count < Filename.Length) text += System.IO.File.ReadAllText(Filename[count++]); // Write the string to a file. System.IO.StreamWriter file = new System.IO.StreamWriter(@"F:\\Test\\Test.txt"); file.Write(text); file.Close(); //Delete the file count = 0; while (count < Filename.Length) File.Delete(Filename[count++]); }

将数据附加到第一个文件本身的代码.正如我先前的答案创建一个新文件一样,但是此解决方案会附加到第一个文件本身. Code which appends data to the first file itself. As my previous answer creates a new file but this solution appends to the first file itself. static void Main(string[] args) { string[] Filename = new string[4] {@"F:\Test\2012_03_05_055030.txt", @"F:\Test\2012_03_05_055035.txt", @"F:\Test\2012_03_05_055040.txt", @"F:\Test\2012_03_05_055045.txt"}; string text=""; int count = 1; while(count < Filename.Length) text += System.IO.File.ReadAllText(Filename[count++]); // Write the string to a file. System.IO.StreamWriter file = new System.IO.StreamWriter(Filename[0]); file.Write(text); file.Close(); //Delete the file count = 1; while (count < Filename.Length) File.Delete(Filename[count++]); }

更多推荐

在C#.net中合并文件

本文发布于:2023-11-06 22:31:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564852.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!