2018年2月7日 星期三
讀寫檔案的另一個選擇 MemoryMappedFile
class FileProcessor
{
public static byte[] Read(string path)
{
byte[] result;
using (var memoryMappedFile = MemoryMappedFile.CreateFromFile(path, FileMode.Open))
{
using (var memoryMappedViewStream = memoryMappedFile.CreateViewStream())
{
result = new byte[memoryMappedViewStream.Length];
memoryMappedViewStream.Read(result, 0, result.Length);
}
}
return result;
}
public static void Write(string path,byte[] content)
{
using (var memoryMappedFile = MemoryMappedFile.CreateFromFile(path, FileMode.Create, "save", content.Length))
{
using (var memoryMappedViewStream = memoryMappedFile.CreateViewStream())
{
memoryMappedViewStream.Write(content, 0, content.Length);
}
}
}
}
class Program
{
static void Main(string[] args)
{
var bytes = FileProcessor.Read("MilkyWay.jpg");
FileProcessor.Write("MilkyWay002.jpg",bytes );
}
}
原始介紹文章出自於此
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言