tmighty
tmighty

Reputation: 11399

ApplicationData.Current.LocalFolder GetFile synchronous

I need to create a database / check for its existance at my program start.

public async Task CopyDatabaseIfNotExists(string dbPath)
{
    var nExpectedFolder = IsolatedStorageFile.GetUserStoreForApplication();

            try
            {
                await ApplicationData.Current.LocalFolder.GetFileAsync(dbPath); //nExpectedFolder.GetFileAsync(dbPath);/// ApplicationData.Current.LocalFolder.GetFileAsync("preinstalledDB.db");
                // No exception means it exists
                return;
            }
            catch (System.IO.FileNotFoundException)
            {
                // The file obviously doesn't exist
            }

        (... some other stuff)

My application can't and shouldn't be run before this test has been completed.

Therefore I wanted to change my calls to be synchronous.

However, I only see

ApplicationData.Current.LocalFolder.GetFileAsync

.GetFile doesn't exist.

What would be the way to make the call synchronous?

Upvotes: 0

Views: 371

Answers (2)

tw2017
tw2017

Reputation: 105

There's no problem with async.

If you do everything async, for example

private async void DoThis()
{
   await SomethingA();
   await SomethingB();

   return;
}

Then you can be sure that SomethingB() will NEVER be called before SomethingA() has been awaited.

That's the reason why you have to do everything async once you do something async in a subroutine.

Upvotes: 1

Peter Torr
Peter Torr

Reputation: 12019

You can use the .NET APIs from System.IO to read files synchronously. You can get the StorageFolder.Path property from the LocalFolder and pass the path name to .NET.

The FileStream constructor takes a path and a mode and from there you can read and write synchronously or asynchronously.

Upvotes: 1

Related Questions