Hello,
It is recommended to use Xamarin.Essentials: File Picker to implement this feature.
This function is mainly divided into two parts: opening a text file and reading the contents of the file.
For opening a text file, you could refer to the following code sample:
var customFileType =
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.Android, new[] { "text/plain" } },
});
var options = new PickOptions
{
PickerTitle = "Please select a text file",
FileTypes = customFileType,
};
var result = await FilePicker.PickAsync(options);
Then, you could read the content from this text file via the following code:
if (result != null && result.FileName.EndsWith("txt", StringComparison.OrdinalIgnoreCase))
{
var content = System.IO.File.ReadAllText(result.FullPath);
label.Text = content;
}
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.