File in memory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ernest Juska
    New Member
    • Aug 2010
    • 5

    File in memory

    Hello,

    I am searching a way to do this:

    1) I have byte array or stream acting a content of file in computer memory (RAM).
    2) I want to make it accessible as regular file on hard disk. The difference is that my file would be absolutely in memory.
    3) That file should be discoverable by Windows Explorer. Also, it could be opened in any binary editor minimaly with read access.

    I would rather prefer a managed way to do that.

    Between, I would like to know if it is possible to shadow in-HDD file with my in-RAM file.

    Thank you.

    P.S: It should be so cheap, so it would free. :)
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    So long story short, you wish to open a file and have only read access? Please elaborate.

    Aimee.

    Comment

    • Ernest Juska
      New Member
      • Aug 2010
      • 5

      #3
      Hope this will be clear:

      1) File (actualy its contents) is in the memory, not in the disk! Got it? I hope so.

      2) I want to make that file to seem like it was in the hard disk. But it wouldnt be there.

      3) It should act like virtual file system. And all that VFS would be entirely in computer RAM.

      ------------------------------------------------------

      Example:

      User runs my app
      He opens drive C: with Windows Explorer
      He opens file test.txt with Notepad (And that test.txt would be provided by application)

      Note:

      test.txt IS NOT in the disk C:, but in RAM. R A M. Clear?
      No File.Save. NO!

      I just somehow need to detect when user opens a file and make it open MY stream EXISTING IN THE MEMORY!

      Thanks.

      Comment

      • Aimee Bailey
        Recognized Expert New Member
        • Apr 2010
        • 197

        #4
        Now don't get angry, but I think your missing a vital piece of a puzzle. RAM is called RAM (or random access memory) as its a place for temporary storage, where your hard disk is a permenant storage facility. Therefor the requirement you have specified dictates that you wish to have a permenant link to something that is inherantly temporary, this is why what you ask is confusing.

        If we were to look at this in a different perspective, you could create your own class based memory mapping system that could include a primitive FAT (File Allocation Table). Then you could create a temporary text file that includes a pseudo path to the pseudo file you are talking about, because the class would exist in memory, effectively you would have what you were asking for.

        Also if you added serialization to the class, you could add some permanence to your file system.

        Comment

        • Ernest Juska
          New Member
          • Aug 2010
          • 5

          #5
          Thank you for better answer. (Cools down)

          Could you provide some example code?

          Thanks.

          Comment

          • Aimee Bailey
            Recognized Expert New Member
            • Apr 2010
            • 197

            #6
            Im not going to write the whole thing for you, but i think its fair i atleast get you started, here is a way you could maintain your own kinda virtual filesystem. What i have done is created a new c# WinForms application, and written the following:

            Code:
                public partial class Form1 : Form
                {
                    VirtualFAT filesys = new VirtualFAT();
            
                    public Form1()
                    {
                        InitializeComponent();
                        filesys.Add(@"c:\image.png");
            
                        VirtualFile file = filesys[0];
                        MessageBox.Show(file.DateCreated.ToString());
                        MessageBox.Show(file.Data.Length.ToString());
                    }
                }
            
            
                public class VirtualFAT : List<VirtualFile>
                {
                    public bool Add(string realFile)
                    {
                        bool result = false;
                        try
                        {
                            byte[] buffer = new byte[0];
                            using (FileStream fs = new FileStream(realFile, FileMode.Open, FileAccess.Read))
                            {
                                buffer = new byte[fs.Length];
                                fs.Read(buffer, 0, Convert.ToInt32(fs.Length));
                            }
                            if (buffer.Length > 0)
                            {
                                this.Add(new VirtualFile(realFile, buffer));
                                result = true;
                            }
                        }
                        catch (IOException ix) { MessageBox.Show(string.Format("Unable to add file {0}\r\n{1}", realFile,ix.Message)); }
                        catch (Exception ex) { MessageBox.Show(string.Format("{0}\r\n\r\n{1}",ex.Message,ex.StackTrace)); }
                        return result;
                    }
                }
            
                public class VirtualFile
                {
                    public string Name;
                    public DateTime DateCreated;
                    public DateTime DateModified;
                    public byte[] Data;
                    public VirtualFile(string realFile, byte[] data)
                    {
                        FileInfo fi = new FileInfo(realFile);
                        Name = fi.Name;
                        DateCreated = DateTime.Now;
                        DateModified = DateTime.Now;
                        Data = data;
                    }
                }
            Note this is very limited, but atleast it shows you what i mean.

            Aimee.

            Comment

            • MrMancunian
              Recognized Expert Contributor
              • Jul 2008
              • 569

              #7
              @Ernest Juska: I urge you to show the Experts some more respect. We are here as a volunteer and helping you is not mandatory. I suggest you change your attitude towards the Experts on this forum if you want an answer to your problem!

              Steven

              Comment

              • Ernest Juska
                New Member
                • Aug 2010
                • 5

                #8
                To above: I will try. :)

                To the above of above:

                Hi,

                One thing. I don't want to load any data from disk:

                MY_BYTES_IN_RAM => ???(BUT NO SAVE TO PHYSICAL DISK) => TEST.TXT => EXPLORER/NOTEPAD/HEXEDITOR/...

                Code:
                    class Program
                    {
                        static void Main()
                        {
                            // I create data here.
                            var data = new byte[1024];
                            new Random().NextBytes(data);
                            var stream = new MemoryStream(data);
                
                            // I construct path to the file here.
                            var filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\test.txt";
                
                            // I make it accessible for Windows Explorer here.
                            // (=_=)??? how?
                
                                // Variant A:
                                // Stamp data from memory straight onto the disk.
                                // Other application would load file from disk. Not from memory. (x_x)
                                using (var file = File.Create(filePath))
                                {
                                    file.Write(data, 0, data.Length);
                                }
                
                                // Variant B:
                                // Create a file server.
                                // Other application would use download file from memory. Changes could be reflected. But its slow. (o_o)
                
                                // Variant C:
                                // Create a virtual drive and associate its volume with data in memory. (O.o)???
                                // No idea how to... Maybe, somewhat to do with DeviceIOControl from kernel32.dll... And CreateFile...
                
                                // Variant D:
                                // (-_-).zZ
                
                            // I run a text editor to open the file here.
                            Process.Start("notepad", "\"" + filePath + "\"");
                
                            // I wait for exit here.
                            Console.ReadLine();
                        }
                    }
                Code:
                var data = (Stream)Make();
                var mFile = new MemoryLinkedFile("C:\test.txt", ref data);
                mFile.Host();
                Console.ReadLine();
                mFile.Quit();
                Thanks.

                Comment

                • Alex Papadimoulis
                  Recognized Expert New Member
                  • Jul 2010
                  • 26

                  #9
                  There is no easy or feasible way to do this.

                  If you want other programs (explorer, notepad, hex editor, etc) to be able to read and see these as "files", then you will need to create something at the operating-system level. That means a driver (specifically, a storage driver)... which even experienced/expert developers find daunting.

                  If you want to see what the driver path is like, here's a good start http://www.microsoft.com/whdc/device...e/default.mspx.

                  Comment

                  • Ernest Juska
                    New Member
                    • Aug 2010
                    • 5

                    #10
                    Found other way...

                    Use custom plugin for Pismo File Mount Audit Package (http://www.pismotechnic.com/download)

                    Thanks.

                    P.S: I will still look at storage drivers. Interesting.

                    Comment

                    Working...