C# 將二進制字符串保存到本地#region 將文件保存到本地
///
/// 將文件保存到本地
///
/// 文件的二進制數據字符串
/// 文件名稱,必須帶后綴
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# 將二進制字符串保存到本地的內容,更多相關內容請關注PHP中文網(www.php.cn)!
本文原創發布php中文網,轉載請注明出處,感謝您的尊重!