Public class ccsetting
{
Public async static void addorupdatevalue <t> (string key, T value)
{
Try
{
If (key! = NULL)
{
Storagefolder floder = applicationdata. Current. localfolder;
If (! (Await floder. getfoldersasync (). Any (A => A. Name = "drieset "))
{
Await applicationdata. Current. localfolder. createfolderasync ("drieset ");
}
String Path = system. Io. Path. Combine ("drieset", key );
Storagefile file = await floder. createfileasync (path, creationcollisionoption. replaceexisting );
Using (Stream stream = await file. openstreamforwriteasync ())
{
Datacontractjsonserializer JS = new datacontractjsonserializer (typeof (t ));
JS. writeobject (stream, value );
}
}
}
Catch
{
}
}
Public async static task <t> getvalueordefault <t> (string key, t defaultvalue)
{
Try
{
Storagefolder floder = applicationdata. Current. localfolder;
String Path = system. Io. Path. Combine ("drieset", key );
If (! (Await floder. getfoldersasync (). Any (A => A. Name = "drieset "))
{
Return defaultvalue;
}
Else
{
Using (Stream istream = await floder. openstreamforreadasync (PATH ))
{
If (istream. length! = 0)
{
Datacontractjsonserializer JS = new datacontractjsonserializer (typeof (t ));
Return (t) Js. readobject (istream );
}
}
}
}
Catch {}
Return defaultvalue;
}
Public async static void removekey (string key)
{
Try
{
Storagefolder floder = applicationdata. Current. localfolder;
String Path = system. Io. Path. Combine ("drieset", key );
If (await checkfileexit (PATH ))
{
Storagefile file = await floder. getfileasync (PATH );
Await file. deleteasync ();
}
}
Catch {}
}
Public async static task <bool> checkfileexit (string filename)
{
Bool B = false;
If (await applicationdata. Current. localfolder. getfoldersasync (). Any (A => A. Name = "drieset "))
{
Storagefolder folder = await applicationdata. Current. localfolder. getfolderasync ("drieset ");
B = (await folder. getfilesasync (). Any (A => A. Name = filename );
}
Else
{
Await applicationdata. Current. localfolder. createfolderasync ("drieset ");
B = false;
}
Return B;
}
}
Internal class folderhelper
{
Const string phonestorage = "C: \ data \ Users \ public \ pictures ";
Const string sdcardstorage = "d :\\";
/// <Summary>
/// Obtain the capacity information of the mobile phone storage device
/// </Summary>
/// <Param name = "type"> storage device type </param>
/// <Returns> storage device capacity Information </returns>
///
/// <Exception CREF = "if the device does not support the SD card, calling the SD memory will result in exception. Remember try... catch..."/>
Public static async task <storagespaceinfo> getstoragespaceinfo (storagetypeenum type)
{
String rootpath;
Switch (type)
{
Case storagetypeenum. sdcard:
Rootpath = sdcardstorage;
Break;
Default:
Rootpath = phonestorage;
Break;
}
Storagefolder folder = await storagefolder. getfolderfrompathasync (rootpath );
Basicproperties properties = await folder. getbasicpropertiesasync ();
Idictionary <string, Object> filteredproperties =
Await properties. retrievepropertiesasync (
New [] {
"System. freespace ",
"System. capacity ",
"System. filecount"
});
VaR capacity = filteredproperties ["system. Capacity"];
If (capacity = NULL)
Return NULL;
VaR freespace = filteredproperties ["system. freespace"];
If (freespace = NULL)
Return NULL;
VaR filecount = filteredproperties ["system. filecount"];
Return new storagespaceinfo (ulong) capacity, (ulong) freespace );
}
}
/// <Summary>
/// Storage device capacity information
/// </Summary>
Internal class storagespaceinfo
{
Public ulong capacitybyte {Get; private set ;}
Public ulong freespacebyte {Get; private set ;}
Public ulong usagebyte {Get; private set ;}
Public String capacitystring {Get; private set ;}
Public String freespacestring {Get; private set ;}
Public String usagestring {Get; private set ;}
Public storagespaceinfo (ulong capacity, ulong freespace)
{
Capacitybyte = capacity;
Freespacebyte = freespace;
Usagebyte = capacity-freespace;
Capacitystring = convertfilesize (capacitybyte );
Freespacestring = convertfilesize (freespacebyte );
Usagestring = convertfilesize (usagebyte );
}
/// <Summary>
/// Convert the file size in bytes into a suitable display string
// Example: 1024000 bytes-> 1000 kb
/// </Summary>
/// <Param name = "size"> file size unit: bytes </param>
/// <Returns> </returns>
String convertfilesize (double size)
{
String unit = "kb ";
Double thresholdvalue = 1024;
Double result = size/thresholdvalue;
// MB
If (result> thresholdvalue)
{
Result = Result/thresholdvalue;
Unit = "MB ";
}
// GB Unit
If (result> thresholdvalue)
{
Result = Result/thresholdvalue;
Unit = "GB ";
}
// TB Unit
If (result> thresholdvalue)
{
Result = Result/thresholdvalue;
Unit = "TB ";
}
Return string. Format ("{0} {1}", result. tostring ("F2"), Unit );
}
}
Enum storagetypeenum
{
Phone,
Sdcard
}