Get values of a file from internal memory android.

I would like to get the values of an txt file, e.g in this file I have the following values:

And I have a form with:

And every time that I change the values of the txt file, I would like to have the new values on the form.

Any idea how to do it?

Thanks in advance

Best Answer

  • NotCoconutNotCoconut ✭✭
    edited April 29 Accepted Answer

    @yelinzh I tried this and it worked, now I can see the values of the txt.

    string path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
    string filename = Path.Combine(path, "hello.txt");

                // Read
                using (var streamReader = new StreamReader(filename))
                {
                    string content = streamReader.ReadToEnd();
                    editorWebservice.Text = content;
    
                    WebPreference.Value = this.FindByName<Editor>("UserName").Text;
                   // System.Diagnostics.Debug.WriteLine(content);
                }
        base.OnAppearing();
    

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited April 28

    @NotCoconut
    You can create a button to update the values when clicking it. Add the updating values code in button click event.

    var file = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "file.txt");
    FindViewById<Button>(Resource.Id.btn3).Click += 
    delegate
    {
        string count = null;
    
        using (var reader = new StreamReader(file, true))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                //update the values of form
            }
        }
    };
    
  • NotCoconutNotCoconut Member ✭✭

    hello there @yelinzh ;

    I tried but there is no way to get acces to that file using "System.Environment.SpecialFolder.Personal". My device is not rooted and I am not able to see that kind of special folders.

    My file is in this level:
    is the helloworld.txt and it contains 1 line the value of the form. Is there another way to get the values from the internal storage? or how can I specify the path for the internal storage?

    Thanks in advance

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited April 29

    @NotCoconut
    Try to get the file by clicking Android -> data (or data/data) -> file_package name -> files

  • NotCoconutNotCoconut Member ✭✭
    edited April 29 Accepted Answer

    @yelinzh I tried this and it worked, now I can see the values of the txt.

    string path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
    string filename = Path.Combine(path, "hello.txt");

                // Read
                using (var streamReader = new StreamReader(filename))
                {
                    string content = streamReader.ReadToEnd();
                    editorWebservice.Text = content;
    
                    WebPreference.Value = this.FindByName<Editor>("UserName").Text;
                   // System.Diagnostics.Debug.WriteLine(content);
                }
        base.OnAppearing();
    
  • JarvanJarvan Member, Xamarin Team Xamurai

    @NotCoconut
    You can mark it as an answer and it'll be helpful for others who have similar problems.

  • NotCoconutNotCoconut Member ✭✭
    edited April 29

    @yelinzh I forgot to mention that I had to put the txt file on the Download folder.

    Thanks for your help. Have a nice day!

Sign In or Register to comment.