首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#接续写入txt文件

2012-08-29 
C#连续写入txt文件1、如何把dataset导入txt2、连续两次导入txt中,。但是,第二次导入的内容会覆盖掉第一次导入

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);            }        }    }} 

热点排行