Forum Cross Platform with Xamarin

How to read txt file on xamarin cross platform (IOS & ANDROID)

So i've had this problem for a bit

I am opening my txt file to text and it only will open on IOS not Android. It just shows an error when i try to open Android.

I put the txt file in the Resource Folder for both IOS and Android but Android won't open it still.

Heres the code for the filepaths:

string filePathq = "questions.txt";

List<string> questions = File.ReadAllLines(filePathq).ToList();

If you could help me with this it would be greatly appreciated

Best Answer

Answers

  • LeloLelo Member ✭✭✭

    But is this when accessing file from inside the library project. Not from the forms project?

  • LeloLelo Member ✭✭✭

    But is this when accessing file from inside the library project. Not from the forms project?

    CurrentClass has to be a class inside the library project. Missed that

  • RajzerRajzer Member ✭✭

    @Yelinzh said:
    To read txt file on xamarin cross platform, you could add the file to the shared project. Then set the Build Action to EmbeddedResource, load the file as resources with the following code on each platform.

    var assembly = IntrospectionExtensions.GetTypeInfo(typeof(CurrentClass)).Assembly;
    Stream stream = assembly.GetManifestResourceStream("ProjectName.FileName.txt");
    string text = "";
    using (var reader = new System.IO.StreamReader(stream))
    {
        text = reader.ReadToEnd();
    }
    

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows#loading-files-embedded-as-resources

    What is CurrentClass? I want to get TTF file which is in the SharedProject in Resources. How to read as stream?

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    It's the page class where you call the function code. You could refer to the sample code:
    https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/workingwithfiles/


    Xamarin Community Forums is moving to Microsoft Q&A!
    For new questions: we invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For existing questions: follow up is still in service until April 15th, 2021.
    For more information, please refer to this sticky post.

Sign In or Register to comment.