Rella
Rella

Reputation: 66985

How to create File with the FILE_ATTRIBUTE_TEMPORARY in C#?

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)

Upvotes: 7

Views: 2472

Answers (3)

Vlad Rudenko
Vlad Rudenko

Reputation: 2859

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);

Upvotes: 1

Tim Lloyd
Tim Lloyd

Reputation: 38494

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

Upvotes: 4

Dean Harding
Dean Harding

Reputation: 72678

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.

Upvotes: 6

Related Questions