六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 1893|回复: 0

C#写入文件加上bom头,主要适用于utf8文件

[复制链接]
 楼主| 发表于 2013-4-26 18:11:43 | 显示全部楼层 |阅读模式
C#写入文件加上bom头,主要适用于utf8文件
此类可结合C#自动识别文件编码类使用
  1. using System;
  2. using System.IO;

  3. public class fileutils{
  4.     /// <summary>
  5.     /// 写入文件加上bom头,主要适用于utf8文件
  6.     /// 欢迎该问http://bbs.agoit.com
  7.     /// </summary>
  8.     /// <param name="newfilepath">文件地址+文件名</param>
  9.     /// <param name="newfilecontent">文件内容</param>
  10.     public static void writefilewidthbom(string newfilepath, string newfilecontent)
  11.     {
  12.         using (FileStream fs = File.OpenWrite(newfilepath))
  13.         {
  14.             //设定书写的开始位置为文件的末尾  
  15.             fs.Position = 0;
  16.             //先写入bom头
  17.             byte[] bomBuffer = new byte[] { 0xef, 0xbb, 0xbf };
  18.             //将待写入内容追加到文件末尾  
  19.             fs.Write(bomBuffer, 0, bomBuffer.Length);
  20.         }

  21.         using (StreamWriter fsw = File.AppendText(newfilepath))
  22.         {
  23.             fsw.WriteLine(newfilecontent);
  24.             fsw.Close();
  25.         }
  26.     }
  27. }
复制代码
该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表