码迷,mamicode.com
首页 > Windows程序 > 详细

C#文本文件或其他文件新内容追加

时间:2015-01-26 16:55:47      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

以txt文本为例,以下代码实现a.txt文件中追加内容

FileStream mystream = new FileStream("C:\\a.txt", FileMode.OpenOrCreate);
            StreamWriter Mywriter = new StreamWriter(mystream, Encoding.Default);
            string text = "";
            for (int i = 0; i < axMSFlexGrid1.Rows; i++)
                for (int j = 0; j < axMSFlexGrid1.Cols; j++)
                    text += flex.get_TextMatrix(i, j) + "\t";
            Mywriter.Write(text);
            Mywriter.Close();
            mystream.Close();

这种方式也可以:

FileStream fs = null;
            string filePath = "C:\\file.txt";
            Encoding encoder = Encoding.UTF8;
            byte[] bytes = encoder.GetBytes("Hello World! \n\r");
            try
            {
                fs = File.OpenWrite(filePath);
               fs.Position = fs.Length;
              fs.Write(bytes, 0, bytes.Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine("文件打开失败{0}", ex.ToString());
            }
            finally
            {
                fs.Close();
            }  


用的最多的还是这种

 StreamWriter sw=File.AppendText("c:\\Text.txt");
 sw.WriteLine("13");
 sw.WriteLine("22222");
 sw.WriteLine("88888");
 sw.Flush();
 sw.Close();

 

C#文本文件或其他文件新内容追加

标签:

原文地址:http://www.cnblogs.com/lsqandzy/p/4250478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!