简体   繁体   English

如何在C#中使用FILE_ATTRIBUTE_TEMPORARY创建文件?

[英]How to create File with the FILE_ATTRIBUTE_TEMPORARY in C#?

How to create File with the FILE_ATTRIBUTE_TEMPORARY in C#? 如何在C#中使用FILE_ATTRIBUTE_TEMPORARY创建文件? (So to store Data in Ram but be able to use it as normal file) (所以将数据存储在Ram中但能够将其用作普通文件)

I believe you'll have to use P/Invoke to call the native CreateFile then use the FileStream(SafeFileHandle, FileAccess) constructor on FileStream . 我相信你将不得不使用P / Invoke来调用本机CreateFile然后使用的FileStream(SafeFileHandle,FileAccess的)构造函数FileStream MSDN has a sample for how to use SafeFileHandle and CreateFile together. MSDN有一个如何一起使用SafeFileHandle和CreateFile 的示例

Memory mapped files are an alternative, and are built into C# 4.0: 内存映射文件是一种替代方案,并内置于C#4.0中:

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

Using P/Invoke is too complex. 使用P / Invoke太复杂了。 Here is a simple way to create such temporary file: 这是创建此类临时文件的简单方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM