示例#1
0
        public static T Load <T>(string path, bool backup = false) where T : class
        {
            string path2  = (!backup) ? UserIO.GetHandle(path) : UserIO.GetBackupHandle(path);
            T      result = default(T);

            try
            {
                if (File.Exists(path2))
                {
                    using (FileStream fileStream = File.OpenRead(path2))
                    {
                        result = UserIO.Deserialise <T>(fileStream);
                    }
                }
                else
                {
                    throw new System.IO.FileNotFoundException("File not found");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.ToString());
            }
            return(result);
        }
示例#2
0
        public static bool Save <T>(string path, byte[] data) where T : class
        {
            string handle = UserIO.GetHandle(path);
            bool   flag   = false;

            try
            {
                string        backupHandle = UserIO.GetBackupHandle(path);
                DirectoryInfo directory    = new FileInfo(handle).Directory;
                if (!directory.Exists)
                {
                    directory.Create();
                }
                directory = new FileInfo(backupHandle).Directory;
                if (!directory.Exists)
                {
                    directory.Create();
                }
                using (FileStream fileStream = File.Open(backupHandle, FileMode.Create, FileAccess.Write))
                {
                    fileStream.Write(data, 0, data.Length);
                }

                //Try loading the save file to test if save was good.
                if (UserIO.Load <T>(path, true) != null)
                {
                    File.Copy(backupHandle, handle, true);
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.ToString());
            }
            if (!flag)
            {
                Console.WriteLine("Save Failed");
            }
            return(flag);
        }