How to store large amount of Data in maui application.

Vaibhav Methuku 80 Reputation points
2024-07-11T05:34:29.1466667+00:00

Hello Team,

We had migrated our xamarin application to Maui - We need to store large json file approx ( 1 Lakh lines).

Earlier in Xamarin we used to create a text file in local folder and we used to write into that particular file. Can we follow the same approach in the MAUI as well or Do we have any better approach than this..

Issue: When we are writing into the particular text file we are facing issues intermittently. Here's the issue.

The process cannot access the file 'C:\Users\name\AppData\Local\Packages\com.myapp_0rsx5m3gg4efp\LocalState\logs.txt' because it is being used by another process.

here's the code for that.

 public partial void Log(string message, string fileName)
 {
     try
     {
         lock (mLocker)
         {
             StorageFolder local = ApplicationData.Current.LocalFolder;
             string documentsPath = local.Path;
             string filePath = Path.Combine(documentsPath, "logs.txt");
             if (!File.Exists(filePath))
             {
                 File.Create(filePath);
             }
             using (StreamWriter streamWriter = new StreamWriter(filePath, true))
             {
                 streamWriter.WriteLine("\r\n" + message);
             }
         }
     }
     catch (IOException ex)
     {
         // Suppressing the exception
         App.TVDAppDiagnosticsService.ReportException(ex);
     }
 }

Thanks
Vaibhav Methuku.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,579 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 118K Reputation points
    2024-07-11T05:57:20.6966667+00:00

    To avoid the intermitent error, try to remove the File.Create line and the whole if.

    0 comments No comments

  2. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 43,926 Reputation points Microsoft Vendor
    2024-07-16T05:46:42.17+00:00

    Hello,

    For how to copy large files in MAUI, you can refer to the general answer of this thread and operate through FileStream.

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.