示例#1
0
 /// <summary>
 /// Saves data to a file.
 /// </summary>
 /// <param name="fileName">Name of the file to write to</param>
 /// <param name="data">The data to save</param>
 public static async Task WriteToDataFileAsync(string directoryName, string fileName, T data)
 {
     try
     {
         string output = JsonConvert.SerializeObject(data);
         await StorageUtility.WriteToDataFileAsync(directoryName, fileName, output);
     }
     catch
     {
         // Add desired error handling for your application
         // ApplicationState.ErrorLog.Add(new ErrorLog("SaveToFile", e.Message));
     }
 }
        /// <summary>
        /// 將物件資料寫入到檔案中
        /// </summary>
        public virtual async Task WriteToFileAsync()
        {
            string data = "";

            if (PersistentStorage == PersistentStorage.Single)
            {
                data = JsonConvert.SerializeObject(this.SingleItem);
            }
            else
            {
                data = JsonConvert.SerializeObject(this.Items);
            }
            await StorageUtility.WriteToDataFileAsync(this.現在資料夾名稱, this.資料檔案名稱, data);
        }
示例#3
0
        /// <summary>
        /// Loads data from a file
        /// </summary>
        /// <param name="fileName">Name of the file to read.</param>
        /// <returns>Data object</returns>
        public static async Task <T> ReadFromFileAsync(string directoryName, string fileName)
        {
            //T loadedFile = default(T);
            T      loadedFile = (T)Activator.CreateInstance(typeof(T));
            string tempStr    = "";

            try
            {
                tempStr = await StorageUtility.ReadFromDataFileAsync(directoryName, fileName);

                loadedFile = JsonConvert.DeserializeObject <T>(tempStr);
            }
            catch
            {
                //ApplicationState.ErrorLog.Add(new ErrorLog("LoadFromFile", e.Message));
            }

            return(loadedFile);
        }
        /// <summary>
        /// 將物件資料從檔案中讀取出來
        /// </summary>
        public virtual async Task ReadFromFileAsync()
        {
            if (PersistentStorage == PersistentStorage.Single)
            {
                SingleItem = (T)Activator.CreateInstance(typeof(T));
            }
            else
            {
                Items = (List <T>)Activator.CreateInstance(typeof(List <T>));
            }

            string data = await StorageUtility.ReadFromDataFileAsync(this.現在資料夾名稱, this.資料檔案名稱);

            if (string.IsNullOrEmpty(data) == true)
            {
            }
            else
            {
                try
                {
                    if (PersistentStorage == PersistentStorage.Single)
                    {
                        this.SingleItem = JsonConvert.DeserializeObject <T>(data, new JsonSerializerSettings {
                            MetadataPropertyHandling = MetadataPropertyHandling.Ignore
                        });
                    }
                    else
                    {
                        this.Items = JsonConvert.DeserializeObject <List <T> >(data, new JsonSerializerSettings {
                            MetadataPropertyHandling = MetadataPropertyHandling.Ignore
                        });
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }