专注

导航

文件与二进制流相互转换

   1:          /// <summary>
   2:          /// 把文件转换成二进制流
   3:          /// </summary>
   4:          /// <param name="Path">输入参数:文件位置</param>
   5:          /// <returns></returns>
   6:          public static byte[] ConvertFileToByte(string Path)
   7:          {
   8:              FileStream stream = new FileInfo(Path).OpenRead();
   9:              byte[] buffer = new byte[stream.Length];
  10:              stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
  11:              return buffer;
  12:          }
  13:   
  14:          /// <summary>
  15:          /// 讲二进制文件转换成文件
  16:          /// </summary>
  17:          /// <param name="data">输入参数:二进制数据流</param>
  18:          /// <param name="Path">输入参数:将要输出文件的位置</param>
  19:          /// <param name="FileType">输入参数:生成文件的类型(word,excel)</param>
  20:          /// <returns>返回生成文件的位置</returns>
  21:          public static string ConvertByteToFile(byte[] data, string Path, string FileType)
  22:          {
  23:              Path = Path + "." + FileType;
  24:              FileStream fs;
  25:              if (System.IO.File.Exists(Path))
  26:              {
  27:                  fs = new FileStream(Path, FileMode.Truncate);
  28:              }
  29:              else
  30:              {
  31:                  fs = new FileStream(Path, FileMode.CreateNew);
  32:              }
  33:              BinaryWriter br = new BinaryWriter(fs);
  34:              br.Write(data, 0, data.Length);
  35:              br.Close();
  36:              fs.Close();
  37:              return Path;
  38:          }

posted on 2011-11-14 13:13  陈啊M  阅读(2593)  评论(0编辑  收藏  举报