当前位置:IT168首页 > 技术开发 > 概述
[收藏此页] [打印] [推荐] [评论]

Filestream和Byte[]之间的转换

责任编辑:nancy作者:ITPUB论坛   2008-07-11   
文本Tag: 微软 sql

【IT168技术文档】

1using System; 2using System.IO; 3using System.IO.Compression; 4 5public class DeflateTest 6{ 7 //读filename到byte[] 8 public static byte[] ReadFile(string fileName) 9 { 10 FileStream fs = new FileStream(fileName,FileMode.OpenOrCreate); 11 byte[] buffer = new byte[fs.Length]; 12 try 13 { 14 fs.Read(buffer, 0, buffer.Length); 15 fs.Seek(0, SeekOrigin.Begin); 16 return buffer; 17 } 18 catch 19 { 20 return buffer; 21 } 22 finally 23 { 24 if (fs != null) 25 fs.Close(); 26 } 27 } 28 //写byte[]到fileName 29 public static bool WriteFile(byte[] pReadByte, string fileName) 30 { 31 FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate); 32 try 33 { 34 fs.Write(pReadByte, 0, pReadByte.Length); 35 } 36 catch 37 { 38 return false; 39 } 40 finally 41 { 42 if (fs != null) 43 fs.Close(); 44 } 45 return true; 46 } 47 48 49 public static void Main(string[] args) 50 { 51 byte[] b = ReadFile(@"d:/a.jpg"); 52 if (WriteFile(b, @"d:/a3.jpg")) 53 { 54 Console.WriteLine("Success"); 55 } 56 else 57 { 58 Console.WriteLine("Sorry"); 59 } 60 Console.ReadLine(); 61 } 62}
上一页
1
下一页
收藏到: 添加到“百度搜藏”添加到“QQ书签”添加到“Google书签”添加到“Yahoo收藏”添加到“和讯网摘”
【内容导航】
本文欢迎转载,转载请注明:转载自IT168 [ http://www.it168.com/ ]
本文链接:http://tech.it168.com/d/2008-07-11/200807111227589.shtml
技术开发相关文章  
  • 暂无
友情推介