抛出异常:' System.UnauthorizedAccessException'在mscorlib.ni.dll中

时间:2016-11-23 01:28:28

标签: c# uwp xamarin.forms onedrive

我正在尝试将Onedrive SDK集成到我的Xamarin应用程序的UWP部分,但是在上传和下载代码中,当到达以下行时,我得到以下异常:

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

    抛出异常:' System.UnauthorizedAccessException'在mscorlib.ni.dll中   例外:访问被拒绝。 (来自HRESULT的异常:0x80070005(E_ACCESSDENIED))mscorlib

    以下是相关代码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;
        }
    

    下载:

      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 + "  ");
            }
        }
    

    请注意我尝试上传的文件类型是.db3文件(SQLite)

    创建Db和其他相关代码如下:

     public class SQliteUWP : ISQlite
    {
        public SQLiteConnection GetSqLiteConnection()
        {
            var sqliteFilename = "E-BusinessCardsSQLite.db3";
            var path = Path.Combine(ApplicationData.Current.LocalFolder.Path, sqliteFilename);
            var connection = new SQLiteConnection(path);
            return connection;
        }
    }
    
      public class EBusinessCardsSqlDatabase
    {
        private SQLiteConnection _sqlDatabase;
        static object _locker = new object();
    
        public EBusinessCardsSqlDatabase()
        {
            _sqlDatabase = DependencyService.Get<ISQlite>().GetSqLiteConnection();
            _sqlDatabase.CreateTable<BusinessCard>();
        }
    
        public IEnumerable<BusinessCard> GetBusinessCards()
        {
            lock (_locker)
            {
                return (from bizCard in _sqlDatabase.Table<BusinessCard>() select bizCard).ToList();
            }
        }
    
        public BusinessCard GetBusinessCard(int cardId)
        {
            lock (_locker)
            {
                return _sqlDatabase.Table<BusinessCard>().FirstOrDefault(bizCard => bizCard.cardID == cardId);
            }
        }
    
        public int DeleteBusinessCard(int cardId)
        {
            lock (_locker)
            {
                return _sqlDatabase.Delete<BusinessCard>(cardId);
            }
        }
    
        public int SaveBusinessCard(BusinessCard card)
        {
            lock (_locker)
            {
                if (card.cardID != 0)
                {
                    _sqlDatabase.Update(card);
                    return card.cardID;
                }
                else
                {
                    return _sqlDatabase.Insert(card);
                }
            }
        }
    }
    

0 个答案:

没有答案