Ejemplo n.º 1
0
        /// <summary>
        /// 创建实体到 LocalFolder中
        /// 若存在则替换
        /// </summary>
        /// <typeparam name="T">数据对象</typeparam>
        /// <param name="t"></param>
        /// <param name="fileName">文件名称</param>
        /// <returns></returns>
        public static async Task CreatEntity <T>(T t, string fileName)
        {
            var folder = ApplicationData.Current.LocalFolder;

            try
            {
                StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

                using (IRandomAccessStream randomAccessSteam = await file.OpenAsync(FileAccessMode.ReadWrite))
                    using (IOutputStream outputStream = randomAccessSteam.GetOutputStreamAt(0))
                    {
                        await XmlSerializationHelper.XMLSerialize(t, outputStream.AsStreamForWrite());

                        await outputStream.FlushAsync();
                    }
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 从LocalFolder ,出文件保存的对象
        /// </summary>
        /// <typeparam name="TResult">返回类型</typeparam>
        /// <param name="fileName">文件名称</param>
        /// <returns></returns>
        public static async Task <TResult> GetContent <TResult>(string fileName) where TResult : class, new()
        {
            StorageFolder folder = ApplicationData.Current.LocalFolder;

            try
            {
                if (await CheckFileExist(folder, fileName))
                {
                    StorageFile file = await folder.GetFileAsync(fileName);

                    using (IRandomAccessStream randomAccessStream = await file.OpenAsync(FileAccessMode.Read))
                        using (IInputStream inPutStream = randomAccessStream.GetInputStreamAt(0))
                        {
                            return(await XmlSerializationHelper.XMLDeserialize <TResult>(inPutStream.AsStreamForRead()));
                        }
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex) { throw ex; }
        }