コード例 #1
0
        public async Task WriteSettingsFile(FinancialManagementSettings settings)
        {
            FileStream   fs = null;
            StreamWriter sw = null;

            try
            {
                string myDocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                fs = new FileStream(myDocs + "\\FMData.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                sw = new StreamWriter(fs, Encoding.Unicode);
                string newValue = JsonConvert.SerializeObject(settings);
                if (File.Exists(myDocs + "\\FMData.txt"))
                {
                    sw.Close();
                    fs.Close();
                    File.WriteAllText(myDocs + "\\FMData.txt", String.Empty);
                    fs = new FileStream(myDocs + "\\FMData.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    sw = new StreamWriter(fs, Encoding.Unicode);
                }

                sw.BaseStream.Position = 0;
                await sw.WriteAsync(newValue);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
        }
コード例 #2
0
 public async Task SetSettings(FinancialManagementSettings settings)
 {
     try
     {
         FileHandler fileHandler = new FileHandler();
         await fileHandler.WriteSettingsFile(settings);
     }
     catch (Exception e)
     {
         throw e;
     }
 }
コード例 #3
0
        public async Task <FinancialManagementSettings> GetSettings()
        {
            try
            {
                FileHandler fileHandler        = new FileHandler();
                FinancialManagementSettings cc = new FinancialManagementSettings();
                cc = await fileHandler.GetStoredSettings();

                return(cc);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
コード例 #4
0
        public async Task <FinancialManagementSettings> GetStoredSettings()
        {
            FinancialManagementSettings storedSettings = null;
            String     folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            FileStream file       = null;

            try
            {
                if (!File.Exists(folderPath + "\\FMData.txt"))
                {
                    FinancialManagementSettings defaultSettings = GetDefaultsConfiguration();
                    await WriteSettingsFile(defaultSettings);

                    storedSettings = defaultSettings;
                }
                else
                {
                    file = new FileStream(folderPath + "\\FMData.txt", FileMode.OpenOrCreate, FileAccess.Read);
                    StreamReader sr           = new StreamReader(file, Encoding.Unicode);
                    string       fileContents = await sr.ReadToEndAsync();

                    storedSettings = JsonConvert.DeserializeObject <FinancialManagementSettings>(fileContents);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (file != null)
                {
                    file.Close();
                }
            }
            return(storedSettings);
        }