Logo

C# byte[]和文件FileStream相互转化

photo

2022年06月01日

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(失败); }

        }


橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它
所有附件
本文为原创文章,请注意保留出处!

热门文章

WordPress 后台编辑器样式实现直接预览 在WordPress3.0以后,有一个新的实用功能:你可以更改默认后台编辑器(TinyMCE)的样...WordPress后台编辑器样式实现直接预览 作者:Pastore Antonio
1533 浏览量
【干货】Chrome插件(扩展)开发全攻略 写在前面我花了将近一个多月的时间断断续续写下这篇博文,并精心写下完整demo,写博客的辛苦大家懂的...【干货】Chrome插件(扩展)开发全攻略 作者:Pastore Antonio
1488 浏览量
memcached 处理 多端口:https://blog.csdn.net/Erica_1230/article/deta...memcached处理 作者:Pastore Antonio
1481 浏览量
使用Nginx+WordPress搭建个人网站 背景很多研究技术的朋友喜欢写博客。如果希望搭建一个完全属于自己的网站,也并不困难。这里简要分享一下...使用Nginx+WordPress搭建个人网站 作者:Pastore Antonio
1453 浏览量
C#图片处理 通常对一幅图片的处理包括:格式变换,缩放(Scale),翻转(Rotate),截取(Clip),滤镜...C#图片处理 作者:Pastore Antonio
1446 浏览量