// ==================================================================== // save to a file // if filepath is not specified, read/save in AppData file // ==================================================================== public void SaveToFile(String filepath = null) { // --------------------------------------------------------------- // if file path not specified, set to last read file // --------------------------------------------------------------- if (filepath == null && DirName != null && FileName != null) { filepath = DirName + "\\" + FileName; } // --------------------------------------------------------------- // just in case filepath doesn't exist, reset path info // --------------------------------------------------------------- _DirName = null; _FileName = null; // --------------------------------------------------------------- // get filepath name (throws exception if it doesn't exist) // --------------------------------------------------------------- filepath = BudgetFiles.VerifyWriteToFileName(filepath, DefaultFileName); // --------------------------------------------------------------- // save as XML // --------------------------------------------------------------- _WriteXMLFile(filepath); // ---------------------------------------------------------------- // save filename info for later use // ---------------------------------------------------------------- _DirName = Path.GetDirectoryName(filepath); _FileName = Path.GetFileName(filepath); }
// ==================================================================== // populate categories from a file // if filepath is not specified, read/save in AppData file // ==================================================================== public void ReadFromFile(String filepath = null) { // --------------------------------------------------------------- // reading from file resets all the current expenses, // so clear out any old definitions // --------------------------------------------------------------- _Expenses.Clear(); // --------------------------------------------------------------- // reset default dir/filename to null // ... filepath may not be valid, // --------------------------------------------------------------- _DirName = null; _FileName = null; // --------------------------------------------------------------- // get filepath name (throws exception if it doesn't exist) // --------------------------------------------------------------- filepath = BudgetFiles.VerifyReadFromFileName(filepath, DefaultFileName); // --------------------------------------------------------------- // read the expenses from the xml file // --------------------------------------------------------------- _ReadXMLFile(filepath); // ---------------------------------------------------------------- // save filename info for later use? // ---------------------------------------------------------------- _DirName = Path.GetDirectoryName(filepath); _FileName = Path.GetFileName(filepath); }
// ==================================================================== // populate categories from a file // if filepath is not specified, read/save in AppData file // ==================================================================== public void ReadFromFile(String filepath = null) { // --------------------------------------------------------------- // reading from file resets all the current categories, // --------------------------------------------------------------- _Cats.Clear(); // --------------------------------------------------------------- // reset default dir/filename to null // ... filepath may not be valid, // --------------------------------------------------------------- _DirName = null; _FileName = null; // --------------------------------------------------------------- // get filepath name (throws exception if it doesn't exist) // --------------------------------------------------------------- filepath = BudgetFiles.VerifyReadFromFileName(filepath, DefaultFileName); // --------------------------------------------------------------- // If file exists, read it // --------------------------------------------------------------- _ReadXMLFile(filepath); _DirName = Path.GetDirectoryName(filepath); _FileName = Path.GetFileName(filepath); }
// ==================================================================== // save to a file // ==================================================================== public void SaveToFile(String filepath) { // --------------------------------------------------------------- // just in case filepath doesn't exist, reset path info // --------------------------------------------------------------- _DirName = null; _FileName = null; // --------------------------------------------------------------- // get filepath name (throws exception if it doesn't exist) // --------------------------------------------------------------- filepath = BudgetFiles.VerifyWriteToFileName(filepath, ""); String path = Path.GetDirectoryName(Path.GetFullPath(filepath)); String file = Path.GetFileNameWithoutExtension(filepath); String ext = Path.GetExtension(filepath); // --------------------------------------------------------------- // construct file names for expenses and categories // --------------------------------------------------------------- String expensepath = path + "\\" + file + "_expenses" + ".exps"; String categorypath = path + "\\" + file + "_categories" + ".cats"; // --------------------------------------------------------------- // save the expenses and budgets into their own files // --------------------------------------------------------------- expenses.SaveToFile(expensepath); categories.SaveToFile(categorypath); // --------------------------------------------------------------- // save filenames of expenses and categories to budget file // --------------------------------------------------------------- string[] files = { Path.GetFileName(categorypath), Path.GetFileName(expensepath) }; System.IO.File.WriteAllLines(filepath, files); // ---------------------------------------------------------------- // save filename info for later use // ---------------------------------------------------------------- _DirName = path; _FileName = Path.GetFileName(filepath); }
// --------------------------------------------------------------- // Read // --------------------------------------------------------------- public void ReadFromFile(String budgetFileName) { // --------------------------------------------------------------- // read the budget file and process // --------------------------------------------------------------- try { // get filepath name (throws exception if it doesn't exist) budgetFileName = BudgetFiles.VerifyReadFromFileName(budgetFileName, ""); // If file exists, read it string[] filenames = System.IO.File.ReadAllLines(budgetFileName); // ---------------------------------------------------------------- // Save information about budget file // ---------------------------------------------------------------- string folder = Path.GetDirectoryName(budgetFileName); _FileName = Path.GetFileName(budgetFileName); // read the expenses and categories from their respective files categories.ReadFromFile(folder + "\\" + filenames[0]); expenses.ReadFromFile(folder + "\\" + filenames[1]); // Save information about budget file _DirName = Path.GetDirectoryName(budgetFileName); _FileName = Path.GetFileName(budgetFileName); } // ---------------------------------------------------------------- // throw new exception if we cannot get the info that we need // ---------------------------------------------------------------- catch (Exception e) { throw new Exception("Could not read budget info: \n" + e.Message); } }