读书人

C#接续写入txt文件

发布时间: 2012-08-29 08:40:14 作者: rapoo

C#连续写入txt文件
1、如何把dataset导入txt

2、连续两次导入txt中,。但是,第二次导入的内容会覆盖掉第一次导入的内容,如何解决?

[解决办法]
File.AppendText
MSDN的例子

C# code
using System;using System.IO;class Test {    public static void Main()     {        string path = @"c:\temp\MyTest.txt";        // This text is added only once to the file.        if (!File.Exists(path))         {            // Create a file to write to.            using (StreamWriter sw = File.CreateText(path))             {                sw.WriteLine("Hello");                sw.WriteLine("And");                sw.WriteLine("Welcome");            }            }        // This text is always added, making the file longer over time        // if it is not deleted.        using (StreamWriter sw = File.AppendText(path))         {            sw.WriteLine("This");            sw.WriteLine("is Extra");            sw.WriteLine("Text");        }            // Open the file to read from.        using (StreamReader sr = File.OpenText(path))         {            string s = "";            while ((s = sr.ReadLine()) != null)             {                Console.WriteLine(s);            }        }    }} 

读书人网 >C#

热点推荐