7

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)

asked May 30, 2010 at 23:42

3 Answers 3

6

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.

answered May 30, 2010 at 23:48
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing out my (C++ to C#) error within answering the question! :)
4

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

answered May 30, 2010 at 23:53

Comments

1

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);
answered Mar 30, 2018 at 12:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.