XNA to Android. Referencing a TXT file

           Afternoon all.

I’m a bit of a novice on this, so please be gentle.

I’m trying to get a level manager that worked ok in XNA to work on
Android. I tested this under Monogame, Windows, OpenGL and worked fine
(obviously, because it has access to the local file system.


So the code looks like this:

string txtline;
            System.IO.StreamReader txtfile;

            cnt = 0;
            string fname = @"C:\Users\Lurch\Desktop\Penetrator2014\WindowsGame1\WindowsGame1Content\" + Global.fileNames[Global.virtualPlayLevelNum];
            txtfile = new System.IO.StreamReader(fname);
            while ((txtline = txtfile.ReadLine()) != null)
                processLine(txtline, cnt);

Now obviously:

  • The problem is the ‘string fname’

  • It is made up of a hard path + a variable which is goverend elsewhere (that bit seems to work as explained further)

  • Obviously it is never going to work because of the hard path.

  • Debugging throws a “could not find the file “/Level1.txt”.” (which
    also tells me the file name variable thats being generated DOES work)

  • So basically, how do I get this to reference the correct file once its deployed the the (emulated) device?

  • I’ve tried simply moving the txt files to the root of the project and ditching the path altogether. No luck there.

I don’t think you should be using hard-coded paths. Anything inside the Content directory with Build Type = Content and Copy set to If Newer (or always) should be accessible to your game, regardless of platform.

I have some sample code running where I have a content.json file setup just like I described above. To read it, I just use System.IO.File.ReadAllText("Content/content.json") and I can access it at runtime.

Oh; I completely agree with hard-coding paths. The level manager was built by my lecturer :confused:
Even if I hard code in the name of the txt file and drop the file name variable, it still barfs.


It was:

string fname = Global.fileNames[Global.virtualPlayLevelNum];
txtfile = new System.IO.StreamReader(TitleContainer.OpenStream(fname));