Find stream path for resource in xamarin

shayshay USMember

I tried to find path for an XML file to read/edit/write it.

At First, I tried in assets but I cant write from it, so I'm trying to find a path for the res directory .

I know that the path for assets is
StreamReader rInereral=new StreamReader("//data//data//MbProject.MbProject//files//invitation.xml");

and I tried to write a similer path for resource :
StreamReader rInereral=new StreamReader("//data//data//MbProject.MbProject//Resource//files//invitation.xml");

But it didn't work. Please help as fast as you can.

Answers

  • shayshay USMember

    i want to correct i'm trying to find a path for the resource diractory

  • JuergenKollerJuergenKoller DEUniversity ✭✭

    I think this is not possible but you could copy the file from the assets folder to a folder where it's possible like:

                var path = "invitation.xml";
                Stream iStream = Application.Context.Assets.Open(path);
                var tempPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                tempPath = System.IO.Path.Combine(tempPath, "appbundlefilestempfolder");
                if (System.IO.Directory.Exists(tempPath) == false)
                {
                    System.IO.Directory.CreateDirectory(tempPath);
                }
                tempPath = System.IO.Path.Combine(tempPath, path);
                //Files from the app package can't change so there is no need to copy them again
                if (System.IO.File.Exists(tempPath) == false)
                {
                    var oStream = new FileOutputStream(tempPath);
                    byte[] buffer = new byte[2048];
                    int length = 2048;
                    while (iStream.Read(buffer, 0, length) > 0)
                    {
                        oStream.Write(buffer, 0, length);
                    }
                    oStream.Flush();
                    oStream.Close();
                    iStream.Close();
                }
    

    In the temp folder 'appbundlefilestempfolder' you should be able to read and write the file as you wish.
    Reading:

    var stream = File.Open(Path, FileMode.Open, System.IO.FileAccess.ReadWrite);
    

    If you need only text and no binary data maybe you can simplify it a bit with something like ReadAllText, WriteAllText, ...
    Hope it helps.

Sign In or Register to comment.