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.