question

KumarAman-0341 avatar image
0 Votes"
KumarAman-0341 asked JarvanZhang-MSFT edited

read txt file from storage device in xamarin?

StreamReader reader = null;
var textFile = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory, "\\USERDETAIL.txt");
CodeContract.Required<Exception>(!File.Exists(Global.mDeviceRootDir + "\\USERDETAIL.TXT"), "USERDETAIL file not found");

         _offlineUsers = new List<OfflineUserDetails>();

         reader = new StreamReader(Global.mDeviceRootDir + "\\USERDETAIL.txt");
         while (!reader.EndOfStream)
         {
             var data = reader.ReadLine();
             string[] strArr = data.Split(',');
             if (strArr.Length == 2)
             {
                 //ListViewItem iItem = new ListViewItem(strArr[0].ToString());
                 //for (int j = 1; j < strArr.Length; j++)
                 //{
                 //    iItem.SubItems.Add(strArr[j].ToString());
                 //}
                 //lvUser.Items.Add(iItem);
             }
         }
         reader.Close();
         reader = null;
dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, KumarAman-0341. Do you face any problem when reading the file? Please post some description about your question. The 'Xamarin.Essentials.FileSystem.AppDataDirectory' command is to get the application's top-level directory. If you want to read the text file in this directory, you could use File.ReadAllText(path); directly.

var mainDir = FileSystem.AppDataDirectory;
var fileName = Path.Combine(mainDir, "test.txt");

string text = File.ReadAllText(fileName);

Here is the related doc, you could refer to:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows#saving-and-loading-files

0 Votes 0 ·

0 Answers