Win8 APP data storage

Source: Internet
Author: User

There are three storage methods for Metro application data

The first is to open a filesavepicker so that the user can select the storage location.

The second method is to obtain the path of the current metro program installation package and save it in the installation path.

// Package. Current. installedlocation. createfileasync (...
// Package. Current. installedlocation. getfileasync (...

The third type is provides access to the application data store.

// Windows. Storage. applicationdata. Current

 

Here I will first introduce how to store data in Application Data Store

First, we will introduce a tool class objectstoragehelper (generic object storage helper for winrt)

The source code is as follows:

Public Enum storagetype
{
Roaming, local, temporary
}

Public class objectstoragehelper <t>
{
Private applicationdata appdata = windows. Storage. applicationdata. Current;
Private xmlserializer serializer;
Private storagetype;

Private string filename (t obj)
{
Return string. Format ("{0}. xml", obj. GetType (). fullname );
}
Public objectstoragehelper (storagetype)
{
Serializer = new xmlserializer (typeof (t ));
Storagetype = storagetype;
}

Public async void deleteasync ()
{
Try
{
Storagefolder folder = getfolder (storagetype );

Var file = await getfileifexistsasync (folder, filename (activator. createinstance <t> ()));
If (file! = NULL)
{
Await file. deleteasync (storagedeleteoption. permanentdelete );
}
}
Catch (exception)
{
Throw;
}
}

Public async void saveasync (t obj)
{
Try
{
If (OBJ! = NULL)
{
Storagefile file = NULL;
Storagefolder folder = getfolder (storagetype );
File = await folder. createfileasync (filename (OBJ), creationcollisionoption. replaceexisting );

Irandomaccessstream writestream = await file. openasync (fileaccessmode. readwrite );
Stream outstream = task. Run () => writestream. asstreamforwrite (). result;
Serializer. serialize (outstream, OBJ );
}
}
Catch (exception)
{
Throw;
}
}
Public async task <t> loadasync ()
{
Try
{
Storagefile file = NULL;
Storagefolder folder = getfolder (storagetype );
File = await folder. getfileasync (filename (activator. createinstance <t> ()));
Irandomaccessstream readstream = await file. openasync (fileaccessmode. Read );
Stream instream = task. Run () => readstream. asstreamforread (). result;
Return (t) serializer. deserialize (instream );
}
Catch (filenotfoundexception)
{
// File not existing is perfectly valid so simply return the default
Return default (t );
// Interesting thread here: how to detect if a file exists (http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/1eb71a80-c59c-4146-aeb6-fefd69f4b4bb)
// Throw;
}
Catch (exception)
{
// Unable to load contents of File
Throw;
}
}

Private storagefolder getfolder (storagetype)
{
Storagefolder folder;
Switch (storagetype)
{
Case storagetype. Roaming:
Folder = appdata. roamingfolder;
Break;
Case storagetype. Local:
Folder = appdata. localfolder;
Break;
Case storagetype. Temporary:
Folder = appdata. temporaryfolder;
Break;
Default:
Throw new exception (string. Format ("unknown storagetype: {0}", storagetype ));
}
Return folder;
}

Private async task <storagefile> getfileifexistsasync (storagefolder folder, string filename)
{
Try
{
Return await folder. getfileasync (filename );

}
Catch
{
Return NULL;
}
}
}

 

Usage:

Public Class Customer
{
Public String customerid {Get; set ;}

Public String customername {Get; set ;}

Public String address {Get; set ;}
}
// Save data
Private void btnsave_click (Object sender, routedeventargs E)
{
VaR customerlist = new list <customer> {
New customer {customerid = "1", customername = "John", address = "benjing "},
New customer {customerid = "2", customername = "Lily", address = "Shanghai "},
New customer {customerid = "3", customername = "Smith", address = "Guangzhou "},
New customer {customerid = "4", customername = "Tom", address = "Shenzhen "}
};

VaR objectstoragehelper = new objectstoragehelper <list <customer> (storagetype. Local );

Objectstoragehelper. saveasync (customerlist );
}

 

// Save data
Private void btnsave_click (Object sender, routedeventargs E)
{
VaR customerlist = new list <customer> {
New customer {customerid = "1", customername = "John", address = "benjing "},
New customer {customerid = "2", customername = "Lily", address = "Shanghai "},
New customer {customerid = "3", customername = "Smith", address = "Guangzhou "},
New customer {customerid = "4", customername = "Tom", address = "Shenzhen "}
};
VaR objectstoragehelper = new objectstoragehelper <list <customer> (storagetype. Local );
Objectstoragehelper. saveasync (customerlist );
}

// Obtain data
Async private void btnshow_click (Object sender, routedeventargs E)
{
VaR objectstoragehelper = new objectstoragehelper <list <customer> (storagetype. Local );
// Get the object from the storage folder
List <customer> customerlist = await objectstoragehelper. loadasync ();
}

 

 

Finally, view the data:

 

 

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.