文件转换成二进制流及二进制流转换成文件
原文发布时间为:2008-08-10 —— 来源于本人的百度文章 [由搬家工具导入]
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
/// <summary>
/// fileToData 的摘要说明
/// </summary>
public class fileToData
{
public fileToData()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static string FileToBinary(string fpath)
{
FileStream fs = new FileStream(fpath, FileMode.Open, FileAccess.Read);
int fileLength = Convert.ToInt32(fs.Length);
byte[] fileBytes = new byte[fileLength];
BinaryReader br = new BinaryReader(fs);
for (int i = 0; i < fileLength; i++)
{
br.Read(fileBytes, 0, fileLength);
}
br.Close();
fs.Close();
string strData = Convert.ToBase64String(fileBytes);
return strData;
}
public static void BinaryToFile(string fpath, string binary)
{
FileStream fs = new FileStream(fpath, FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(Convert.FromBase64String(binary));
bw.Close();
fs.Close();
}
}