C#如何把二進位字串儲存到本地

來源:互聯網
上載者:User
#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

本文URL:http://www.bianceng.cn/Programming/csharp/201410/45587.htm

假如檔案流儲存在資料庫中:

string vsSql = "";//從資料庫中擷取待轉換儲存檔案的內容(比如,之前把檔案轉換為位元組流儲存到資料庫中了)

DataSet dsContent = 擷取DataSet的資料庫操作;

byte[] vbContent = (byte[])(dsContent.Tables[0].Rows[0]["資料庫中儲存檔案內容的列名"]);

string vsContent = Convert.ToBase64String(vbContent);

位元組流儲存在資料庫中的樣子:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.