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

Question
-
User395357 posted
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
questions = File.ReadAllLines(filePathq).ToList(); ``` If you could help me with this it would be greatly appreciated
Thursday, June 25, 2020 12:05 AM
Answers
-
User382871 posted
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
- Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
Thursday, June 25, 2020 2:18 AM
All replies
-
User382871 posted
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
- Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
Thursday, June 25, 2020 2:18 AM -
User372105 posted
But is this when accessing file from inside the library project. Not from the forms project?
Saturday, January 30, 2021 9:28 AM -
User372105 posted
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 thatSaturday, January 30, 2021 9:43 AM -
User392574 posted
@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?
Tuesday, March 16, 2021 8:42 AM -
User382871 posted
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.
Wednesday, March 17, 2021 1:53 AM