-2

I am a noob to C#.

So, needed a small help from stackoverflow community.

I want to create a directory structure in memory (not storage device) and place some files into it (for security and faster access purpose).

Later some other process/method wants to access these directories and read the files. I think this can be achieved with some memory buffer, but I'm not able to implement it in C#.

Please help !

Thanks in advance.

amritkv88
  • 17
  • 3
  • 2
    A 'directory structure in memory' is just any kind of tree structure that you set up in memory. It is only a 'directory structure' of folders, sub-folders, and files when it's written into the file system. In that sense, if you don't want files on the disk, just keep the data in memory that you can share with other objects. You want to look at the classes that inherit from the `Stream` class. In particular `MemoryStream` and `FileStream`: https://learn.microsoft.com/en-us/dotnet/api/system.io.stream?view=net-6.0 – dmedine Aug 25 '22 at 04:46
  • Related: [Windows - Programmable RAM Disk API for .NET?](https://stackoverflow.com/questions/2402888/programmable-ram-disk-api-for-net) – John Wu Aug 25 '22 at 05:59
  • Related: [How to implement shared memory in .NET](https://stackoverflow.com/questions/439787/how-to-implement-shared-memory-in-net) – John Wu Aug 25 '22 at 06:03
  • Do the files have to be accessible using normal file APIs? if so you want a RAM disk. Otherwise just create a nested tree of dictionaries, which you can use to do simple lookups – Charlieface Aug 25 '22 at 06:14

2 Answers2

1

You cannot write files to the file-system without actually writing them to a storage device. Note that there are ram-drives that act as storage devices, while keeping the files in memory.

If you just want to lookup some data by a string-key you can just use a Dictionary<string, MemoryStream>, and while it would be difficult to make access much faster, the data would only be accessible from your process. Another option could be a zip-archive, that kind of works like a dictionary, but also compresses the contents.

I'm really unsure what you mean with "security", whenever talking about security you really need to define what kind of threat you want to protect against. Writing data to disk does not automatically makes it more secure, if anything it would be made less secure since it is much easier to read data from disk than from memory of another process.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • Actually there are couple of files those are protected using password. Once I open it using password and if I save it to a disk (for future use of the file content), the contents of the file will be visible to the user. This is the thing I want to hide and want to open the file in main memory only for some time, until it is consumed by other method let's say. – amritkv88 Aug 25 '22 at 11:15
  • 2
    @AmritKumarVerma, sure, files can be encrypted and password protected, but that is not applied by default. You can also encrypt data in memory. In general, if an attacker has full access to your process, he can do whatever you are doing. So there is little point in trying to protect against an attacker that already have admin permissions on the computer. – JonasH Aug 25 '22 at 14:39
1

you can check this piece of code and modify as you require.

[HttpPost]
public IActionResult Upload(SingleFileModel model)
{
if (ModelState.IsValid)
{
    model.IsResponse = true;

    string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Files");

    //create folder if not exist
    if (!Directory.Exists(path))
        Directory.CreateDirectory(path);

    //get file extension
    FileInfo fileInfo = new FileInfo(model.File.FileName);
    string fileName = model.FileName + fileInfo.Extension;

    string fileNameWithPath = Path.Combine(path, fileName);

    using (var stream = new FileStream(fileNameWithPath, FileMode.Create))
    {
        model.File.CopyTo(stream);
    }
    model.IsSuccess = true;
    model.Message = "File upload successfully";
}