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);
位元組流儲存在資料庫中的樣子:
以上就是C# 將二進位字串儲存到本地的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!