How To: Create a File
Demonstrates how to use the StorageContainer class to create a save game file in the title storage area on a device specified by the gamer. This example assumes you obtained a StorageDevice. To obtain a StorageDevice, see How To: Get a StorageDevice Asynchronously.
The Complete Sample
The code in the topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.
Creating a File
To create a save game file in title storage
-
Call the Guide.BeginShowStorageDeviceSelector method to get a device index indicating which device the user prefers.
-
Open a StorageContainer on the device, and then pass the name of your title.
-
Call Path.Combine to merge the container path with the name of the file to be created.
-
Call File.Exists to confirm that the file is not already present.
-
Call File.Create with the file name of the file to create.
File.Create returns a FileStream object that you can use to add data to the file.
-
Call Close to close the file after you have populated it with data.
-
Call StorageContainer to commit the changes to the device.
private static void DoCreate(StorageDevice device) { // Open a storage container. StorageContainer container = device.OpenContainer("StorageDemo"); // Add the container path to our file name. string filename = Path.Combine(container.Path, "demobinary.sav"); // Create a new file. if (!File.Exists(filename)) { FileStream file = File.Create(filename); file.Close(); } // Dispose the container, to commit the data. container.Dispose(); }