question

Voytec-6484 avatar image
0 Votes"
Voytec-6484 asked NicoZhu-MSFT commented

UWP C# Reading/writing files process - why it takes so long...

Hey,

I wanted to ask if it's normal if reading a file takes long time (~0.25 sec) for each .dat file? Each file has just few sentences.
I use this code:

 StorageFolder folderSP = await KnownFolders.DocumentsLibrary.GetFolderAsync("Św. Puszcza App");
 try { StorageFolder folderDATA = await folderSP.GetFolderAsync(data_str); }
 catch { Windows.Storage.StorageFolder folderDATA = await folderSP.CreateFolderAsync(data_str, CreationCollisionOption.ReplaceExisting); }
 StorageFolder nowyfolderDATA = await folderSP.GetFolderAsync(data_str);
 try { StorageFile plik = await nowyfolderDATA.GetFileAsync(pokoj.Value + ".dat"); }
 catch { Windows.Storage.StorageFile plik = await nowyfolderDATA.CreateFileAsync(pokoj.Value + ".dat", CreationCollisionOption.ReplaceExisting); }
 Windows.Storage.StorageFile plikPOKOJ = await nowyfolderDATA.GetFileAsync(pokoj.Value + ".dat");
 string text = await Windows.Storage.FileIO.ReadTextAsync(plikPOKOJ);
 var buffer = await Windows.Storage.FileIO.ReadBufferAsync(plikPOKOJ);
 using (var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(buffer))
 {
          string readText = dataReader.ReadString(buffer.Length);
 }


windows-uwp
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.

1 Answer

NicoZhu-MSFT avatar image
1 Vote"
NicoZhu-MSFT answered NicoZhu-MSFT commented

Hello, Welcome to Micorosoft Q&A,

UWP C# Reading/writing files process - why it takes so long.

Above code contains make folder and if the the specific file was not exist, and it will consume more time. I found you use two ways to read file content.
One is FileIO.ReadTextAsync read content. it suit for loading the smallfile directly. and the other is using buffer, it suit for loading the big file, and it has higher performance, you could chose one of the them, please don't use it together that will consume more time. For more detail please refer to File Accessing Document and Best practices for writing to files.


If the response is helpful, please click "Accept Answer" and upvote it.
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.


· 7
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.

Thanks @NicoZhu-MSFT
Make a respond so I can mark it as right answer.

0 Votes 0 ·

It didn't help @NicoZhu-MSFT , same loading time... ;(

0 Votes 0 ·

How do you record the loading time?

0 Votes 0 ·
Show more comments