2
votes
private async Task<StorageFile> GetCsvFile()
        {
            var localFolder = KnownFolders.DocumentsLibrary;
            var file = await localFolder.CreateFileAsync("NRBcatalogue.csv", Windows.Storage.CreationCollisionOption.ReplaceExisting);

            String rk = "";
            for (int i = 0; i < k1.Count; i++)
            {
                rk += k1[i] + "\n";   
            }
            await Windows.Storage.FileIO.WriteTextAsync(file, rk);
            return file;
        }

        private async void AppBarButton_Click_1(object sender, RoutedEventArgs e)
        {
            EmailMessage email = new EmailMessage();
            email.To.Add(new EmailRecipient("[email protected]"));
            email.Subject = "NRB Catalogue";
            var file = await GetCsvFile(); //Error occured here
            email.Attachments.Add(new EmailAttachment(file.Name, file));
            await EmailManager.ShowComposeNewEmailAsync(email);
        }

The Error Details are: A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll. An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll but was not handled in user code. Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

1
Does your application have permission to access the documents library?DavidG

1 Answers

2
votes

you are trying to access a location that you don't have permission var localFolder = KnownFolders.DocumentsLibrary;

This is valid exception because you can't access DocumentsLibrary location from you windows phone app. This location is only available for Windows store app. You can use other location but before using make sure that you have added this location as capability in the app’s manifest. For reference check This Link.

So have to chose other location that your app can access. e.g LocalFolder , IsolatedStorage etc. For Localfolder just change your accessing folder code by below code.

 var localFolder =  Windows.Storage.ApplicationData.Current.LocalFolder;

Hope it solve your problem. cheers :)