.net 获取文件格式
2010-01-19 16:00
225 查看
System.Text.Encoding enc; using (System.IO.FileStream file = new System.IO.FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { if (file.CanSeek) { byte[] bom = new byte[4]; // Get the byte-order mark, if there is one file.Read(bom, 0, 4); if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) { enc = System.Text.Encoding.UTF8; } else if (bom[0] == 0xff && bom[1] == 0xfe) { // ucs-2le, ucs-4le, and ucs-16le enc = System.Text.Encoding.Unicode; } else if (bom[0] == 0xfe && bom[1] == 0xff) { // utf-16 and ucs-2 enc = System.Text.Encoding.BigEndianUnicode; } else if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff) { // ucs-4 enc = System.Text.Encoding.UTF32; } else { enc = System.Text.Encoding.Default; } } else { // The file cannot be randomly accessed, so you need to decide what to set the default to // based on the data provided. If you're expecting data from a lot of older applications, // default your encoding to Encoding.ASCII. If you're expecting data from a lot of newer // applications, default your encoding to Encoding.Unicode. Also, since binary files are // single byte-based, so you will want to use Encoding.ASCII, even though you'll probably // never need to use the encoding then since the Encoding classes are really meant to get // strings from the byte array that is the file. enc = System.Text.Encoding.Default; } return enc; }
相关文章推荐
- .NET json格式 使用Newtonsoft.Json.JsonConvert类 附读取文件方法
- java 获取文件编码格式
- 根据PE文件格式获取LoadLibraryA()/GetProcAddress()地址
- Java 实现获取指定文件夹下的指定格式的所有文件
- 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本
- js格式文件中获取JSP变量的方法
- .net 多文件上传,限制文件大小和格式
- c# 获取路径文件中的多种文件格式的文件
- .net导出EXCEL文件操作类包括格式较复杂表格导出
- .NET(C#):使用IResourceReader,IResourceWriter和ResourceSet获取资源文件
- .NET获取指定文件夹下的所有文件
- Java如何获取文件编码格式
- .NET中使用oledb连接sybase和csv格式文件的连接
- Java如何获取文件编码格式
- Java如何获取文件编码格式
- php获取文件夹中所有lrc格式文件,提取lrc文件中内容
- Java如何获取文件编码格式
- c# 获取MP3和AMR文件格式的时长
- .NET文件格式相关开源项目
- Android之获取证书文件(pfx格式)的公钥和私钥