// Project: XnaGraphicEngine, File: FileHelper.cs
// Namespace: XnaGraphicEngine.Helpers, Class: FileHelper
// Path: C:\code\XnaGraphicEngine\Helpers, Author: Abi
// Code lines: 137, Size of file: 4,13 KB
// Creation date: 15.10.2006 09:08
// Last modified: 22.10.2006 18:03
// Generated with Commenter by abi.exDream.com
#region Using directives
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
#endregion
namespace XnaGraphicEngine.Helpers
{
///
/// File helper class to get text lines, number of text lines, etc.
/// Update: Now also supports the XNA Storage classes :)
///
public class FileHelper
{
#region LoadGameContentFile
///
/// Load game content file, returns null if file was not found.
///
/// Relative filename
/// File stream
public static FileStream LoadGameContentFile(string relativeFilename)
{
string fullPath = Path.Combine(
StorageContainer.TitleLocation, relativeFilename);
if (File.Exists(fullPath) == false)
return null;
else
return File.Open(fullPath,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
} // LoadGameContentFile(relativeFilename)
#endregion
#region OpenOrCreateFileForCurrentPlayer
///
/// XNA user device, asks for the saving location on the Xbox360,
/// theirfore remember this device for the time we run the game.
///
static StorageDevice xnaUserDevice = null;
///
/// Xna user device
///
/// Storage device
public static StorageDevice XnaUserDevice
{
get
{
// Create if not created yet.
if (xnaUserDevice == null)
xnaUserDevice =
StorageDevice.ShowStorageDeviceGuide(PlayerIndex.One);
return xnaUserDevice;
} // get
} // XnaUserDevice
///
/// Open or create file for current player. Basically just creates a
/// FileStream using the specified FileMode flag, but on the Xbox360
/// we have to ask the user first where he wants to.
/// Basically used for the GameSettings and the Log class.
///
/// Filename
/// Mode
/// Access
/// File stream
public static FileStream OpenFileForCurrentPlayer(
string filename, FileMode mode, FileAccess access)
{
// Open a storage container
StorageContainer container = XnaUserDevice.OpenContainer("XnaGraphicEngine");
// Add the container path to our filename
string fullFilename = Path.Combine(container.Path, filename);
// Opens or creates the requested file
return new FileStream(
fullFilename, mode, access, FileShare.ReadWrite);
} // OpenFileForCurrentPlayer(filename, mode, access)
#endregion
#region Get text lines
///
/// Returns the number of text lines we got in a file.
///
static public string[] GetLines(string filename)
{
try
{
StreamReader reader = new StreamReader(
new FileStream(filename, FileMode.Open, FileAccess.Read),
System.Text.Encoding.UTF8);
// Generic version
List lines = new List();
do
{
lines.Add(reader.ReadLine());
} while (reader.Peek() > -1);
reader.Close();
return lines.ToArray();
} // try
catch
{
// Failed to read, just return null!
return null;
} // catch
} // GetLines(filename)
#endregion
#region Create text file
///
/// Create text file
///
/// Filename
/// Text for file
///
/// Will be thrown if file already exists.
///
public static void CreateTextFile(
string filename, string textForFile,
Encoding fileEncoding)
{
StreamWriter textWriter = new StreamWriter(
new FileStream(filename, FileMode.Create, FileAccess.Write),
fileEncoding);//System.Text.Encoding.UTF8);
string[] textLines = StringHelper.SplitMultilineText(textForFile);
foreach (string line in textLines)
textWriter.WriteLine(line);
textWriter.Close();
} // CreateTextFile(filename, textForFile)
#endregion
} // class FileHelper
} // namespace XnaGraphicEngine.Helpers