閱讀706 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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);

字節流保存在數據庫中的樣子:


最後更新:2017-04-03 12:55:27

  上一篇:go vs2010 修改編譯生成dll存放路徑
  下一篇:go C# FileStream.Read Method