zoukankan      html  css  js  c++  java
  • C# byte[]和文件FileStream相互转化

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

            }

  • 相关阅读:
    css中后代、元素、类、id选择器以及行间style优先级的比较
    JS小功能x系列6文字自动滚动
    JS小功能系列7自动打字
    JS小功能系列6折叠
    JS小功能系列5图片左右移动
    JS小功能系列4图片轮播综合数字轮播,顺时针逆时针,自动轮播
    JS小功能系列3时钟
    JS小功能系列2商品计算
    JS小功能系列1换一批
    JS隔行变色,鼠标悬停变色
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/3573189.html
Copyright © 2011-2022 走看看