Exceptions et gestion des fichiers : sauvegarder et charger un niveau
CHAPITRE 8
177
Lire le contenu d’un fichier
public class ChapitreHuit : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
StorageDevice device;
StorageContainer container;
public ChapitreHuit()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.Components.Add(new GamerServicesComponent(this));
}
protected override void Initialize()
{
base.Initialize();
Guide.BeginShowStorageDeviceSelector(new AsyncCallback(GetDevice), null);
}
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.L) && File.Exists
➥(Path.Combine(container.Path, "test.sav")))
{
try
{
FileStream file = File.Open(Path.Combine(container.Path,
➥"test.sav"), FileMode.Open);
StreamReader fileReader = new StreamReader(file);
Window.Title = fileReader.r();
fileReader.Close();
}
catch (Exception e)
{
Guide.BeginShowMessageBox(PlayerIndex.One, "Erreur", e.Message,
➥new string[] { "Ok" }, 0, MessageBoxIcon.Error, new
➥AsyncCallback(EndShowMessageBox), null);
}
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
base.Draw(gameTime);
}
=Labat FM.book Page 177 Vendredi, 19. juin 2009 4:01 16
CHAPITRE 8
177
Lire le contenu d’un fichier
public class ChapitreHuit : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
StorageDevice device;
StorageContainer container;
public ChapitreHuit()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.Components.Add(new GamerServicesComponent(this));
}
protected override void Initialize()
{
base.Initialize();
Guide.BeginShowStorageDeviceSelector(new AsyncCallback(GetDevice), null);
}
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.L) && File.Exists
➥(Path.Combine(container.Path, "test.sav")))
{
try
{
FileStream file = File.Open(Path.Combine(container.Path,
➥"test.sav"), FileMode.Open);
StreamReader fileReader = new StreamReader(file);
Window.Title = fileReader.r();
fileReader.Close();
}
catch (Exception e)
{
Guide.BeginShowMessageBox(PlayerIndex.One, "Erreur", e.Message,
➥new string[] { "Ok" }, 0, MessageBoxIcon.Error, new
➥AsyncCallback(EndShowMessageBox), null);
}
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
base.Draw(gameTime);
}
=Labat FM.book Page 177 Vendredi, 19. juin 2009 4:01 16
