Forum Visual Studio

XNA to Android. Referencing a TXT file

JasonWeberJasonWeber AUMember
edited February 2015 in Visual Studio

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.


  • VitaliyTerzievVitaliyTerziev USMember
    edited March 2015

    Why not put the txt files into the assets folder, where they should be, and after that

    using (StreamReader sr = new StreamReader (Application.Context.Assets.Open (any.txt))) IF into the Activity file using (StreamReader sr = new StreamReader (Assets.Open (any.txt))) ah this may not be the answer for you if this is not standart Xamarin.Android APP , then Ignore it

  • JasonWeberJasonWeber AUMember

    I ended up working out.
    string fname = Global.fileNames[Global.virtualPlayLevelNum];
    txtfile = new System.IO.StreamReader(TitleContainer.OpenStream(fname));

Sign In or Register to comment.