Forum Xamarin.Forms

Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll

PaulaAliuPaulaAliu USMember ✭✭
edited November 2016 in Xamarin.Forms

I am trying to integrate the Onedrive SDK to the UWP Portion of my Xamarin App, However in the Upload and Download code, I get the following exception when ever the following lines are reached:

  1. using (var stream = await sqliteStorageFileFile.OpenStreamForReadAsync())
  2. using (IRandomAccessStream appFileStream = await appFile.OpenAsync(FileAccessMode.ReadWrite))

Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll An Exception: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) mscorlib

Here is the relevant code snippnets:

UPLOAD:

   public async Task Upload(string filename)
{
    await InitializeClient();
    StorageFolder localFolder = ApplicationData.Current.LocalFolder;
    sqliteStorageFileFile = await localFolder.GetFileAsync(filename);

   try
    {
        using (var stream = await sqliteStorageFileFile.OpenStreamForReadAsync())
        {
            //Debug.WriteLine("QW"); Debug.WriteLine("QW"); Debug.WriteLine("QW");
            var item = await OneDriveClient.Drive.Special.AppRoot.ItemWithPath(sqliteStorageFileFile.Name)
                        .Content.Request().PutAsync<Item>(stream);
            Debug.WriteLine(item.Id + "Id of item");
        }

    }
    catch (Exception e)
    {
        Debug.WriteLine(e.Message + " " + e.Source);
    }

    return;
}

DOWNLOAD:

public async Task Download(string filename)
{
await InitializeClient();
try
{
var filebuilder = OneDriveClient.Drive.Special.AppRoot.ItemWithPath(filename);
var backedUpFile = await filebuilder.Request().GetAsync();
var fileStream = await filebuilder.Content.Request().GetAsync();
//Byte Creation for fileStream
byte[] fileBytes;
IRandomAccessStream stream = fileStream.AsRandomAccessStream();
using (DataReader reader = new DataReader(stream))
{
fileBytes = new byte[stream.Size];
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(fileBytes);
}

        IStorageFile appFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename,
       CreationCollisionOption.OpenIfExists);

        using (IRandomAccessStream appFileStream = await appFile.OpenAsync(FileAccessMode.ReadWrite))
        {

            using (IOutputStream outputStream = appFileStream.GetOutputStreamAt(0))
            {
                using (DataWriter writer = new DataWriter(outputStream))
                {
                    writer.WriteBytes(fileBytes);
                    await writer.StoreAsync();
                    writer.DetachStream();
                }
                await outputStream.FlushAsync();
            }
        }
    }
    catch (Exception e )
    {
        Debug.WriteLine("An Exception: " + e.Message + "  " + e.Source + "  ");
    }
}

Please note the type of file I am trying to upload is a .db3 file (SQLite)

Sign In or Register to comment.