朋友们, 如何在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中合并文件
发布评论