开发者

How to create File with the FILE_ATTRIBUTE_TEMPORARY in C#?

开发者 https://www.devze.com 2023-01-01 08:41 出处:网络
How to create File with the FILE_开发者_运维问答ATTRIBUTE_TEMPORARYin C#? (So to store Data in Ram but be able to use it as normal file)I believe you\'ll have to use P/Invoke to call the native Create

How to create File with the FILE_开发者_运维问答ATTRIBUTE_TEMPORARY in C#? (So to store Data in Ram but be able to use it as normal file)


I believe you'll have to use P/Invoke to call the native CreateFile then use the FileStream(SafeFileHandle, FileAccess) constructor on FileStream. MSDN has a sample for how to use SafeFileHandle and CreateFile together.


Memory mapped files are an alternative, and are built into C# 4.0:

http://msdn.microsoft.com/en-us/library/system.io.memorymappedfiles.memorymappedfile.aspx


Using P/Invoke is too complex. Here is a simple way to create such temporary file:

var file = File.Create(path);
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Temporary);
0

精彩评论

暂无评论...
验证码 换一张
取 消