C# byte[]和文件FileStream相互转化
来源:互联网 发布:java小游戏代码文件 编辑:程序博客网 时间:2024/05/18 09:25
using System.IO;
//读filename到byte[]
private byte[] ReadFile(string fileName)
{
FileStream pFileStream = null;
byte[] pReadByte = new byte[0];
try
{
pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(pFileStream);
r.BaseStream.Seek(0, SeekOrigin.Begin); //将文件指针设置到文件开
pReadByte = r.ReadBytes((int)r.BaseStream.Length);
return pReadByte;
}
catch
{
return pReadByte;
}
finally
{
if (pFileStream != null)
pFileStream.Close();
}
}
//写byte[]到fileName
private bool writeFile(byte[] pReadByte, string fileName)
{
FileStream pFileStream = null;
try
{
pFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
pFileStream.Write(pReadByte, 0, pReadByte.Length);
}
catch
{
return false;
}
finally
{
if (pFileStream != null)
pFileStream.Close();
}
return true;
}
测试
private void button6_Click(object sender, EventArgs e)
{
//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.11.23
byte[] b = ReadFile(@"c:/u.jpg");
if (writeFile(b, @"c:/u1.jpg"))
{
MessageBox.Show("成功");
}
else { MessageBox.Show("失败"); }
}
- C# byte[]和文件FileStream相互转化
- C# byte[]和文件FileStream相互转化
- byte[]和文件FileStream相互转化
- Java byte数组和文件相互转换
- C# string类型和Byte[]相互转化
- byte数组和文件读写
- C# double float int string 与 byte数组 相互转化
- C# Bitmap类型与Byte[]类型相互转化
- C# double float int string 与 byte数组 相互转化
- C# double float int string 与 byte数组 相互转化
- C# double float int string 与 byte数组 相互转化
- BipMap.byte[],drawable相互转化
- Bitmap,byte[],Drawable相互转化
- Bitmap,byte[],Drawable相互转化
- Bitmap,byte[],Drawable相互转化
- String和byte[]相互转化
- Bitmap,byte[],Drawable相互转化
- byte[]与ByteBuffer相互转化
- 多鼠标技术的应用,请有多个鼠标的兄弟帮我测试一下
- 精通Hibernate之映射继承关系三
- T-SQL语句还原数据库
- 精通Hibernate之映射继承关系四(图)
- 设置全屏Symbian C++应用程序
- C# byte[]和文件FileStream相互转化
- 如何学习JAVA
- SVG(Scalable Vector Graphics可缩放矢量图形)格式介绍
- 爱走了 心该静了
- Servlet过滤器机制分析及应用(二)
- Google AdSense申请攻略(转)
- 在WPF中,如何得到任何Object对象的XAML代码?
- 试验网站#1搜索引擎优化收录情况记录(断续运行)2007-11-23
- 试验网站#3搜索引擎优化收录情况记录2007-11-23