0

I am writing to a file, then reading it back right after:

await FileIO.WriteTextAsync(file, command, Windows.Storage.Streams.UnicodeEncoding.Utf8);   
var readFile = await ApplicationData.Current.TemporaryFolder.GetFileAsync(WEB_LOG);

However, GetFileAsync throws a FileNotFoundException because the file hasn't appeared yet. If I use the debugger and wait a little after WriteTextAsync finishes, I can see the file appear in the folder and GetFileAsync does not throw an exception. How can I wait for the file to be fully written to and appear in the folder so that GetFileAsync does not throw an exception?

ShrimpCrackers
  • 4,388
  • 17
  • 50
  • 76
  • I couldn't find anything in the Microsoft's docs to be notified when the OS finishes writing a file and I guess the most basic solution is to implement a method that check periodically if the file has been created. Does this help: https://stackoverflow.com/a/1406853/1817574 ? –  Oct 24 '22 at 06:06
  • Or this one https://stackoverflow.com/a/876513/1817574 –  Oct 24 '22 at 06:11
  • 1
    Do you really *have* to save the file and read it back? That is best avoided whenever possible. – JonasH Oct 24 '22 at 06:36
  • I just realized this issue is not due to UWP, and the code above would probably work well. I have a better understanding of the code I'm working with. What is happening is that a file is created in C:\ drive. UWP can't access it unless using a FilePicker (use of which is not possible for this app), so a Win32 agent is copying the contents of the file into the accessible UWP Known Folders upon reading a command that the UWP app creates and the Win32 agent polls for. My only option I think is to just await a certain amount of time using a Task. – ShrimpCrackers Oct 24 '22 at 07:07

2 Answers2

2

You could try to use StorageFolder.TryGetItemAsync(String) to check if the file is ready. This method returns null instead of raising a FileNotFoundException if the specified file or folder is not found.

Like this:

 var target=  await ApplicationData.Current.TemporaryFolder.TryGetItemAsync("FileName");

        if (target != null)
        {
            // file is ready
        }
        else 
        {
            //wait and try to get the file again
        }
Roy Li - MSFT
  • 8,043
  • 1
  • 7
  • 13
0

There is no function, which will allow you to wait on a particular file to be available for reading. You have to check if it is not ready then wait...

 await FileIO.WriteTextAsync(file, command, Windows.Storage.Streams.UnicodeEncoding.Utf8); 

        while (true)
        {
            try
            {
                using (FileStream inputStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None))
                    if (inputStream.Length > 0)
                        break;
            }
            catch (Exception)
            {
                await Task.Delay(50);
            }
        }

var readFile = await ApplicationData.Current.TemporaryFolder.GetFileAsync(WEB_LOG);
Hossein Sabziani
  • 1
  • 2
  • 15
  • 20