//Creates folder on users computer where data will be saved private void DoSaveGame(StorageDevice a_device, Game a_game, string a_file) { try { // Open a storage container. IAsyncResult f_result = a_device.BeginOpenContainer("Cubes_are_acute", null, null); // Wait for the WaitHandle to become signaled. f_result.AsyncWaitHandle.WaitOne(); StorageContainer container = a_device.EndOpenContainer(f_result); // Close the wait handle. f_result.AsyncWaitHandle.Close(); //Path is Documents/SavedGames/Cubes_are_acute on C: string filename = a_file; String[] f_str = container.GetFileNames(); if (filename == "Empty") { filename = "Save" + (f_str.Length + 1); } else if (filename == "New Save") { filename = "Save" + (f_str.Length + 1); } else { filename = a_file; } // Check to see whether the save exists. if (container.FileExists(filename)) { // Delete it so that we can create one fresh. container.DeleteFile(filename); } // Create the file. Stream stream = container.CreateFile(filename); // Convert the object to Binary data and put it in the stream. BinaryFormatter binFormatter = new BinaryFormatter(); //serializes Game and writes it's data to savegame.sav binFormatter.Serialize(stream, a_game); // Close the file. stream.Close(); // Dispose the container, to commit changes. container.Dispose(); } catch (Exception) { throw; } }
//Returns the number of existing savefiles private String[] GetSaves(StorageDevice a_device) { try { IAsyncResult f_result = a_device.BeginOpenContainer("Cubes_are_acute", null, null); // Wait for the WaitHandle to become signaled. f_result.AsyncWaitHandle.WaitOne(); StorageContainer container = a_device.EndOpenContainer(f_result); // Close the wait handle. f_result.AsyncWaitHandle.Close(); string[] filename = container.GetFileNames(); container.Dispose(); return(filename); } catch (Exception) { throw; } }
/// <summary> /// Gets an array of all files available in a container. /// </summary> /// <param name="containerName">The name of the container in which to search for files.</param> /// <param name="pattern">A search pattern to use to find files.</param> /// <returns>An array of file names of the files in the container.</returns> public string[] GetFiles(string containerName, string pattern) { VerifyIsReady(); // lock on the storage device so that only one storage operation can occur at a time lock (_lock) { // open a container using (StorageContainer currentContainer = OpenContainer(containerName)) { return(string.IsNullOrEmpty(pattern) ? currentContainer.GetFileNames() : currentContainer.GetFileNames(pattern)); } } }
void BuildSavesList() { if ((null == evdEnGlobals.Storage) || !evdEnGlobals.Storage.IsConnected) { ExitScreen(); } try { IAsyncResult result = evdEnGlobals.Storage.BeginOpenContainer(evdEnGlobals.GameName, null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = evdEnGlobals.Storage.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); string[] FileList = container.GetFileNames("save?.evden"); foreach (string filename in FileList) { FileStream ff = (FileStream)container.OpenFile(filename, FileMode.Open); string ss = ff.Name; ff.Close(); string s = Path.GetFileNameWithoutExtension(filename); if (s.Length == 5) { s = s.Substring(4); int i; if (int.TryParse(s, out i)) { // we got our candidate DateTime dt = File.GetLastWriteTime(ss); slot[i].Text = dt.ToString("s").Replace('T', ' '); slot[i].ToolTip = filename; } } else { // not real save... where it came from??? } } } catch { } }
/// <summary> /// Asynchronous storage-device callback for /// refreshing the save-game descriptions. /// </summary> private static List <SaveGameDescription> RefreshSaveGameDescriptionsResult(StorageDevice storageDevice) { if (storageDevice == null) { throw new ArgumentNullException("storageDevice"); } if (!storageDevice.IsConnected) { throw new InvalidOperationException("Cannot connect to storage device."); } List <SaveGameDescription> saveGameDescriptions = new List <SaveGameDescription>(); // open the container using (StorageContainer storageContainer = OpenContainer(storageDevice)) { // get the description list string[] filenames = storageContainer.GetFileNames("SaveGameDescription*.xml"); // add each entry to the list foreach (string filename in filenames) { SaveGameDescription saveGameDescription; // check the size of the list if (saveGameDescriptions.Count >= SaveLoadScreen.MaximumSaveGameDescriptions) { break; } // open the file stream using (Stream fileStream = storageContainer.OpenFile(filename, FileMode.Open)) { // deserialize the object saveGameDescription = sSaveGameDescriptionSerializer.Deserialize(fileStream) as SaveGameDescription; // if it's valid, add it to the list if (saveGameDescription != null) { saveGameDescriptions.Add(saveGameDescription); } } } } return(saveGameDescriptions); }
/// <summary> /// Get the list of files in the container directory. /// </summary> /// <returns>List of files in the container directory.</returns> public static string[] GetFilesInDirectory() { return(_container.GetFileNames()); }