简体   繁体   中英

C# FileSavePicker Error Metro App

I am writing an app (for Windows 8.1 in C#, a Metro App) that will have resources included in the app's assets. I want the user to be able to save these, so I have a button that lets them pick a location and save the file. I use lots of file types, some of which are included (I didn't get round to finishing adding filetypes as I gave it a test). I get the following error:

An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code

Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

This is my code:

FileSavePicker saver = new FileSavePicker();
        saver.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        saver.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
        saver.FileTypeChoices.Add("PowerPoint Presentation", new List<string>() { ".ppt" });
        saver.FileTypeChoices.Add("Executable file", new List<string>() { ".exe" });
        saver.FileTypeChoices.Add("PDF", new List<string>() { ".pdf" });
        saver.DefaultFileExtension = ".docx";
        saver.SuggestedFileName = "New Document";
        StorageFile file = await saver.PickSaveFileAsync();
        if (null != file)
        {
            var filey = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(ViewUri.Text);
            await Windows.Storage.FileIO.WriteBytesAsync(filey, new byte[4000]);
            //Saved, tell the user
        }
        else
        {
            //Error, tell the user
        }

The 'ViewUri.Text' shouldn't be the error as it opens the file in another section of the code (ViewUri essentially displays the location of the file). The app crashes on the away Windows.... line. I have the following questions, and would love any help:

  1. What is the correct usage for the second argument for WriteBytesAsync? new byte[4000] was just a guess.

  2. Is it possible to save a file with most filetypes (so every supported file type is listed, and they have to pick the right type, and it will save) or do I have to check what the file they are saving is and then save it under different conditions?

  3. What is causing the error (if not the byte[4000] part)?

Any help is appreciated. Thanks!

I finally cracked it! So whilst it doesn't involve letting the user save the file as, it does let them choose where to copy it to & copy it to there. Here was my solution, in case anyone else was stumped by this (I really couldn't find much information of this sort of thing anywhere):

FolderPicker picker = new FolderPicker();
        picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        picker.FileTypeFilter.Add(".pdf");
        StorageFolder fold = await picker.PickSingleFolderAsync();

        StorageFile filey = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(ViewUri.Text);
        StorageFolder storageFolder = fold;
        try
        {
            await filey.CopyAsync(storageFolder);
            // let them know, done!
        }
        catch (System.UnauthorizedAccessException)
        {
            // an error occurred
        }

Essentially, it asks the user where they want to save. Then it copies the file into that location. Replace ViewUri.Text with a string location of the file, such as Assets\\mine.pdf

This also works with all(?) filetypes - at least to my knowledge.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM