[英]How do I convert an .sdf to binary file using C#?
我需要將.sdf文件轉換為二進制文件。 (我認為使用streamreader嗎?)然后我需要將此二進制文件轉換回.sdf。 我該怎么做呢?
該文件本身已經是二進制文件,因此我假設您想將文件作為二進制文件讀入內存。 試一下:
public byte[] ReadBinaryFile(string path)
{
byte[] data;
using (System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open))
{
data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
}
return data;
}
如果要將此byte[]
寫回到文件中,請執行以下操作:
public void WriteBinaryFile(string path, byte[] data)
{
using (System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Create))
{
stream.Write(data, 0, data.Length);
}
}
我已經對此進行了更新,以將邏輯封裝為兩個函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.