Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm working on porting a game from PC to Xbox 360.

When I want to create a level I read a .txt file that I store in my content I read the whole file as a string using this method

File.ReadAllText(fi.FullName);

Where I'm passing in the full path to that file.

Later on in my code I parse that string to create whatever level I need.

When I try to deploy my game onto the Xbox I usually get an error stating

'System.IO.File' does not contain a definition for 'ReadAllText'

I was wondering if there was a simple way to read the text in a .txt file when I deploy onto the Xbox 360

share|improve this question
2  
    
It's tangential to your question, but putting your text into a simple XML wrapper would allow you to use the content pipeline. msdn.microsoft.com/en-us/library/ff604982.aspx Although there is no technical rule against storing content data in the title container, using binary content instead would improve loading time, conserve (a tiny bit of) space, and allow you to keep save games away from vital game content files. As a best practice, content goes with the game, title storage is for auxiliary storage (saves, user settings, etc.) –  Seth Battin Apr 16 '13 at 5:14

1 Answer 1

up vote 3 down vote accepted

You can't access the file system on the Xbox 360 in the same you can on Windows; you need you use one of the storage APIs to access files.

There's an article here which outlines the different ways to handle IO in XNA 4.0 which will work on both the Xbox 360 and Windows. From a quick read it looks like you want to use Title Container, for which I've included some example code below (taken from the MSDN)

/// <summary>
/// This method opens a file using System.IO classes and the
/// TitleLocation property.  It presumes that a file named
/// ship.dds has been deployed alongside the game.
/// </summary>
private static void DoOpenFile()
{
    try
    {
        System.IO.Stream stream = TitleContainer.OpenStream("ship.dds");
        System.IO.StreamReader sreader = new System.IO.StreamReader(stream);
        // use StreamReader.ReadLine or other methods to read the file data

        Console.WriteLine("File Size: " + stream.Length);
        stream.Close();
    }
    catch (System.IO.FileNotFoundException)
    {
        // this will be thrown by OpenStream if gamedata.txt
        // doesn't exist in the title storage location
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.