how to write a text file in Maui

Sojan 21 Reputation points
2022-09-02T02:42:28.57+00:00

how can we create text file using maui and access it?

How to write a text file somewhere on emulator or device in public or app folder?

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

2 answers

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,261 Reputation points Microsoft Vendor
    2022-09-05T02:49:15.053+00:00

    Hello,

    how can we create text file using maui and access it?
    How to write a text file somewhere on emulator or device in public or app folder?

    It is recommended to write/read a file in the app folder. You can use File system helpers to write/read text file.

    Please check the following code to write text to a text file in the app folder. 

       public async void WirteTextToFile(string text, string targetFileName)  
                       {  
               // Write the file content to the app data directory  
               string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, targetFileName);  
               using FileStream outputStream = System.IO.File.OpenWrite(targetFile);  
               using StreamWriter streamWriter = new StreamWriter(outputStream);  
               await streamWriter.WriteAsync(text);  
           }  
    

    Please check the following code to read text from text file of app folder.

       public async Task<string> ReadTextFile(string targetFileName)  
           {  
               string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, targetFileName);  
               using FileStream InputStream = System.IO.File.OpenRead(targetFile);  
               using StreamReader reader = new StreamReader(InputStream);  
               return await reader.ReadToEndAsync();  
           }  
    

    Best Regards,

    Leon Lu


    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.

    1 person found this answer helpful.

  2. Mielesplayz 281 Reputation points
    2022-09-05T10:25:43.98+00:00

    This tutorial shows you how you can make a notepad with a save function and delete function, is that what you are looking for?
    https://learn.microsoft.com/en-us/learn/modules/create-user-interface-xaml/

    0 comments No comments