C # 's independent storage and mapped memory

Source: Internet
Author: User

Both stand-alone and mapped memory are used to handle program data problems. Independent storage can be used to temporarily save some of the program's unimportant data, mapped memory files can be used to solve the program data reading problems. When a program needs to load a large amount of external data in the process of running, it is a good choice to use it as a data cache area.

Independent storage

static void Userisolationfile () {//this file is saved into the path:c:\users\you Account\appdata\loc  
      
    Al\isolatedstorage IsolatedStorageFile storfile = Isolatedstoragefile.getuserstorefordomain (); IsolatedStorageFileStream Storstream = new IsolatedStorageFileStream ("Storagefile.txt", FileMode.Create,  

    FileAccess.Write);  
      
    StreamWriter writer = new StreamWriter (Storstream); Writer.  
      
    WriteLine ("You are dead!"); Writer.  
      
    Flush (); Writer.  
      
    Close ();  
      
    Storstream.close ();  

    Storfile.close ();  
      
    IsolatedStorageFile storFile2 = Isolatedstoragefile.getuserstorefordomain ();  

    string[] filenames = Storfile2.getfilenames ();  
      
        foreach (string filename in filenames) {if (filename!= "Storagefile.txt")  
      
        {continue; using (IsolatedStorageFileStream stream = new IsolatedstoRagefilestream ("Storagefile.txt", FileMode.Open)) {using (StreamReader reader = new StreamReader (Stream)) {Console.WriteLine (reader).  
      
            ReadToEnd ());  
      
}} console.readkey (); }

Mapping memory

How does this thing feel like C ah, haha. If a program requires frequent read and write text class files can this it to do a read-write cache to improve the efficiency of the program.

static void Mappingmemory ()  
      
{   
      
using (var mmfile=memorymappedfile.createfromfile ("D:\\mappingmemory.txt")
      
, FileMode.Create  
      
, "FileHandle", 1024*1024))  
      
    {  
      
        String valuetowrite = ' written to ' mapped-memory file on + DateTime.Now.ToString ();  
      
        var myaccessor = Mmfile.createviewaccessor ();  

        Myaccessor.writearray<byte> (0, Encoding.ASCII.GetBytes (valuetowrite), 0  
      
, valuetowrite.length);  

        var readout = new Byte[valuetowrite.length];  
      
        Myaccessor.readarray<byte> (0, readout, 0, readout.length);  
      
        Console.WriteLine ("The Data is:" + Encoding.ASCII.GetString (readout));  
      
        Console.readkey ();  

    }  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.