Got it. For some strange reason, the synchronous methods of the System.IO namespace behave differently (in terms of streaming) for disk folders in different places.
It works:
FileStream fs = new FileStream(Path.Combine(ApplicationData.Current.LocalFolder.Path, "log.txt"), FileMode.OpenOrCreate);
The message “System.InvalidOperationException” and the message “Synchronous operations should not be performed on the user interface thread are displayed below. Consider transferring this method to Task.Run ':
FileStream fs = new FileStream(@"C:\Temp\log.txt", FileMode.OpenOrCreate);
At the same time, if I run the latter in the workflow, and not in the user interface thread, I get an exact UnauthorizedAccessException telling me that Access is denied.
those. it seems that the prohibition of starting synchronous operations with the user interface stream does NOT apply to folders (at least ApplicationData.Current.LocalFolder), where this I / O is allowed and applies to folders where this I / O is not allowed in any case. Maybe the error in the .NET code for UWP was selected due to a wrong exception?
Strange, since I believe that this should not be any relationship between the streaming and access control processes, but at least I found a way to collapse the UWP synchronization method as I needed.
EDIT:
It has the correct and expected behavior. When WinRT does not even try to access resources outside LocalFolder in the user interface stream, because it will cause an internal deadlock (this is how WinRT works under the hood). For LocalFolder access, a user interface stream is allowed for I / O synchronization. This is why I get an InvalidOperationException (rather than an UnauthorizedAccessException) in the UI thread only when accessing resources outside of LocalFolder.
The problem with the UI thread will be with an object similar to a file collector that can return a location outside of LocalFolder (broker file). Access to such a file in the workflow will not lead to a failure of UnauthorizedAccessException and it will be fine, but in the user interface thread we will get an InvalidOperationException because this file is outside of LocalFolder. Thus, broker files can only be accessed from the workflow (either through asynchronous methods, or using synchronization methods in a dedicated workflow).