c#文本文件写入

  string err = "Hellod world";
            FileStream fs = null;
            string filePath = HttpContext.Current.Server.MapPath("~/Error.txt");
            if (!File.Exists(filePath))
            {
                using (fs = File.Create(filePath)){ }
            }
            //将待写的入数据从字符串转换为字节数组
            Encoding encoder = Encoding.UTF8;
            byte[] bytes = encoder.GetBytes(err);
            fs = File.OpenWrite(filePath);
            //设定书写的开始位置为文件的末尾
            fs.Position = fs.Length;
            //将待写入内容追加到文件末尾
            fs.Write(bytes, 0, bytes.Length);
            fs.Dispose();
            fs.Close();
posted @ 2016-04-20 17:56  Pavilion  阅读(232)  评论(0编辑  收藏  举报