Beispiel #1
0
        public void SaveGame()
        {
            if (!Directory.Exists(SAVE_FOLDER_NAME))
            {
                Directory.CreateDirectory(SAVE_FOLDER_NAME);
            }

            BinaryFormatter formatter = new BinaryFormatter();
            FileStream      saveFile  = File.Create(SAVE_FOLDER_NAME + dispatcher.DataCollector.Name + SAVE_FILE_EXT);

            dispatcher.GetData();

            localData = dispatcher.DataCollector;

            formatter.Serialize(saveFile, localData);

            isGameSaved = true;

            saveFile.Close();
        }
Beispiel #2
0
        public List <DataCollector> GetSaves()
        {
            List <DataCollector> dataCollectors = new List <DataCollector>();
            BinaryFormatter      formatter      = new BinaryFormatter();
            List <FileStream>    filesList      = new List <FileStream>();
            var filesNames = Directory.GetFiles(SAVE_FOLDER_NAME).Where(file => !file.ToLower().Contains("desktop.ini")); //Inspired from : nerdshark https://www.reddit.com/r/csharp/comments/7uulwg/get_all_items_from_desktop_except_desktopini_am_i/

            nbOfSaves = 0;

            foreach (var filesName in filesNames)
            {
                filesList.Add(File.Open(filesName, FileMode.Open));
                nbOfSaves++;
            }

            foreach (var file in filesList)
            {
                localData = (DataCollector)formatter.Deserialize(file);
                dataCollectors.Add(localData);
                file.Close();
            }

            return(dataCollectors);
        }