2

I apologize if this is a dupe. I couldnt find anything that dealt specifically with my issue. Im trying to save a file using "await SaveAsync". I get this error Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) I assume its because it attempted to move on before an action completed... but shouldnt it wait because I used "await"?

Here is my code

try
        {
            if (CurrentFile == null || !_existingDocument)
            {
                await _generateDoc();
                return;
            }

            var docGen = new CustDocument(_inputs);

            docGen.Save(CurrentFile);
        }
        catch (Exception ex)
        {

            throw;
        }

And my method

 private async Task<StorageFile> _generateDoc()
    {

        var docGen = new CustDocument(_inputs);
        var savePicker = new FileSavePicker();

        savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        savePicker.FileTypeChoices.Add("dptx File", new List<string>() {".dptx"});
        savePicker.SuggestedFileName = _inputs.CustomerName.Replace(" ", "").Trim();

        var file = await savePicker.PickSaveFileAsync(); // Error here

        docGen.Save(file);
        return file;
    }

Stack Track (Not Much)

   at Windows.Storage.Pickers.FileSavePicker.PickSaveFileAsync()

at MdTool.ViewModel.RmViewModel.<_generateDoc>d__6.MoveNext()

  • 9
    You're not awaiting *saving* the file - you're awaiting *picking* the filename, by the looks of it... Now, why have you assumed that the problem is that `await` hasn't worked properly? And is the error *actually* within `PickSaveFileAsync` or is it in `Save`? – Jon Skeet May 22 '15 at 14:29
  • Hi Jon, I assume that because code immediately throws the error at that point. On web findings I saw a similar issue where the error was due to the async code continuing to run before the file save had completed. So the answer is PickSaveFileAsync. – CodeInColor May 22 '15 at 19:17
  • 1
    I suggest you don't assume anything. But you should post the full stack trace in your question. – Jon Skeet May 22 '15 at 19:27
  • That good advise :) I would never completely assume anything. Just speculating on a hunch... Adding the stack trace to the top. Not much there. – CodeInColor May 22 '15 at 19:32
  • 2
    Well given that the exception is in PickSaveFileAsync itself, you never get as far as awaiting it as far as I can see... – Jon Skeet May 22 '15 at 19:36
  • I suggest you try to come up with an absolutely minimal repro of it. – Jon Skeet May 22 '15 at 19:44
  • @JonSkeet yes, he especially needs to delete all code that never runs. Confusing right now. – usr May 22 '15 at 19:53
  • Are calling on a background thread? You can only can this method from the UI thread. – Peter Torr - MSFT May 22 '15 at 19:55
  • I hadnt considered that Peter. It is calling from a background thread. Thanks for the extra pair of eyes guys. I appreciate it. – CodeInColor May 24 '15 at 14:06

1 Answers1

0

FOUND IT! Here Using FileSavePicker with MessageDialog's IUICommand event

Would have never thought that it had to do with the dialog box. To all who responded... Thanks for the help

Community
  • 1
  • 1