C# 将二进制字符串保存到本地

        #region 将文件保存到本地
        /// <summary>
        /// 将文件保存到本地
        /// </summary>
        /// <param name="psContent">文件的二进制数据字符串</param>
        /// <param name="psFileName">文件名称,必须带后缀</param>
        private void SaveFile(string psContent, string psFileName)
        {
            byte[] accessory = Convert.FromBase64String(psContent);
            //System.AppDomain.CurrentDomain.BaseDirectory获取程序的基目录
            string vsAccessoryPath = System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + '\\' + psFileName;
            FileStream fileStream = null;
            try
            {
                //File.Create Method (String):Creates or overwrites a file in the specified path.
                fileStream = File.Create(vsAccessoryPath);
            }
            catch (System.IO.IOException e)
            {
                
            }
            //FileStream.Write Method:Writes a block of bytes to the file stream.
            fileStream.Write(accessory, 0, accessory.Length);
            //FileStream.Flush 方法:清除该流的所有缓冲区,使得所有缓冲的数据都被写入到基础设备。
            fileStream.Flush();
            //FileStream.Close Method:Closes the file and releases any resources associated with the current file stream.
            fileStream.Close();
        }
        #endregion

假如文件流保存在数据库中:

string vsSql = "";//从数据库中获取待转换保存文件的内容(比如,之前把文件转换为字节流保存到数据库中了)
DataSet dsContent = 获取DataSet的数据库操作;
byte[] vbContent = (byte[])(dsContent.Tables[0].Rows[0]["数据库中保存文件内容的列名"]);
string vsContent = Convert.ToBase64String(vbContent);

字节流保存在数据库中的样子:


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值