question

PaulRyan-4569 avatar image
0 Votes"
PaulRyan-4569 asked ·

UWP StrorageFileGetFilePathAsync()

HI, I am trying to figure out how I run this synch

private async Task ReadPtFileAsync( )
{
string path = @"c:\Users\pryan\OneDrive\MUA_Data\Patients.json";
var file = await StorageFile.GetFileFromPathAsync(path);
json = await FileIO.ReadTextAsync(file);
pts.PtList = JsonConvert.DeserializeObject<List<Patient>>(json);
}

thanks for the help

windows-uwp
· 1
10 |1000 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.

I'm confused about "how I run this synch", do you mean you do not know how these asynchronous statements work in ReadPtFileAsync method? Or did you encounter issues when you called this method? Can you show more details about your unexpected behavior and expected behavior?

0 Votes 0 · ·
danielescipioni avatar image
0 Votes"
danielescipioni answered ·

Try with

var file = StorageFile.GetFileFromPathAsync(path).GetAwaiter().GetResult();
string json = FileIO.ReadTextAsync(file).GetAwaiter().GetResult();


· 1 · Share
10 |1000 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.

Thank you

0 Votes 0 · ·
yanxiaodi avatar image
0 Votes"
yanxiaodi answered ·

The StorageFile API can only reads/writes the files in some specific folders, like LocalState, PictureLibrary, etc. Please see the document here: https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions

If you want to read files out of these folders, you need to declare the broadFileSystemAccess capability in the app manifest or call the file picker.

Example:

 <Package
   ...
   xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
   IgnorableNamespaces="uap mp rescap">
 ...
 <Capabilities>
     <rescap:Capability Name="broadFileSystemAccess" />
 </Capabilities>

From your code, I guess you are trying to read the files in OneDrive so you may need more permissions for this folder.

You could use a try-catch block to wrapper your code and see if there are any exceptions.

· 1 · Share
10 |1000 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, I have no issue reading the file. The issue I am running into is that the StorageFile.... and FileIO are async --- I need them to run in sequential order, is there a way to enforce sequential

0 Votes 0 · ·