0

I am trying to get values from a specified route to load it in a collectionView. I don't know why Xamarin add a / to my path at the beggining. I am desesperate, I've tried to replace the /, differents ways to obtain the path, in differents places (inside the project, in the desktop) and any works. I think the problem could be the MonoAndroid file or some dll but I am not sure.

Differents things I've tried: 1:

StreamReader sr = new StreamReader("./ficheroPruebaXautoCB.txt");

        string linea;

        // Read the file and display it line by line.  
        while ((linea = sr.ReadLine()) != null)
        {
            ubicaciones.Ubication = linea.Substring(27, 33);
            ubicaciones.Values = linea.Substring(0, 26) + linea.Substring(34, 48);

            listUbicaciones.Add(ubicaciones);
            counter++;
        }
        sr.Close();

2:

string path = "F:\\Xamarin\\Pruebas_fichero\\ficheroPruebaXautoCB.txt";
            string path2 = @"F:\Xamarin\Pruebas_fichero\ficheroPruebaXautoCB.txt";
            foreach (string line in System.IO.File.ReadLines(path2))
            {
                ubicaciones.Ubication = line.Substring(27, 33);
                ubicaciones.Values = line.Substring(0, 26) + line.Substring(34, 48);

                listUbicaciones.Add(ubicaciones);
                counter++;
            }

3:

 string nombreArchivo = "ficheroPruebaXautoCB.txt";
        string ruta = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
        string rutaCompleta = Path.Combine(ruta, nombreArchivo);

        if (File.Exists(rutaCompleta))
        {
            using (var lector = new StreamReader(rutaCompleta, true))
            {
                string textoLeido;
                while ((textoLeido = lector.ReadLine()) != null)
                {
                    ubicaciones.Ubication = textoLeido.Substring(27, 33);
                    ubicaciones.Values = textoLeido.Substring(0, 26) + textoLeido.Substring(34, 48);

                    listUbicaciones.Add(ubicaciones);
                    counter++;
                }
            }
        }

4:

string ruta = "C:\\Users\\H_L\\Desktop\\ficheroPruebaXautoCB.txt";
        try
        {

            //FileStream f = File.OpenRead(ruta);
            var text = File.ReadLines(ruta, Encoding.UTF8);

            foreach (string line in text)
            {
                ubicaciones.Ubication = line.Substring(27, 33);
                ubicaciones.Values = line.Substring(0, 26) + line.Substring(34, 48);

                listUbicaciones.Add(ubicaciones);
            }



        }
        catch (IOException e)
        {
            Console.WriteLine(e.Message);
        }

And the error:

"Could not find file "/C:\Users\H_L\Desktop\ficheroPruebaXautoCB.txt""

All these solutions have worked in a console C# project, so the problem is when I try to do it with Xamarin.

HeLu
  • 1
  • 1
  • 1
    please keep in mind that your app is designed to run on a mobile device completely independent of your PC. Therefor at runtime you can't rely on any files that exist on your PC drive. Any file you need to access at runtime needs to be included in the app bundle so that it is deployed to the device during build. – Jason May 23 '22 at 11:22
  • Add the text file to your project as an embedded resource. [File Handling / Embedded Resource](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows#loading-files-embedded-as-resources). Rt-click on project, Add Existing item, change type at bottom of file picker to show any file, select the file. Click on file name in Solution Explorer, look at Properties / BuildAction = EmbeddedResource. – ToolmakerSteve May 23 '22 at 21:57

1 Answers1

1

It seems that you can't access the file in your pc with the path for the android. You need to provide a path which the Android can use.

At first, you need to copy the local file into the simulator. You can check the following link : How to access local files of the filesystem in the Android emulator?

And then, if the file is in the app's own folder, you can access it without any storage permission. But when the file is in the other folders, you need to grant the read and write storage permission to your app.

You can check my old answer in the following link:How to open SQLite DB created by one Xamarin Android app in another app?

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14