locked
Read from file(Deserialize) in winrt RRS feed

  • Question

  • Please What is the problem of this code? why it cause:

    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))

    public async Task RestoreAsync()
    {    
        StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("UserInfo");    
        if (file == null) return ;
        IRandomAccessStream inStream = await file.OpenReadAsync();                      
        // Deserialize the Session State.
        DataContractSerializer serializer = new DataContractSerializer(typeof(UserInfo));
        CurrentUser = (UserInfo)serializer.ReadObject(inStream.AsStreamForRead());
        inStream.Dispose();
        this.DefaultViewModel["FirstName"] = CurrentUser.F_Name;
        this.DefaultViewModel["LastName"] = CurrentUser.L_Name;
    }

    Wednesday, December 18, 2013 3:27 PM

All replies

  • Because the file you are trying to get is not present there.

    If the content you are trying to load is in your Project Solution you should access it first by a uri and then you can store it in localstorage.

    To access from uri try this

     StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///somefile.txt"));

    Wednesday, December 18, 2013 5:21 PM
  • I am doing this,when the use When the user logs in

    UserInfo CurrentUser = new UserInfo();
    CurrentUser.F_Name = CurrentObject.Get<string>("F_Name");
    CurrentUser.F_Name = CurrentObject.Get<string>("F_Name");
    CurrentUser.L_Name = CurrentObject.Get<string>("L_Name");
    CurrentUser.Company_Name = 
    CurrentUser.Password = CurrentUserPass;
    
     StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                                    StorageFile userdetailsfile = await localFolder.CreateFileAsync("UserInfo", CreationCollisionOption.ReplaceExisting);
                                    IRandomAccessStream raStream = await userdetailsfile.OpenAsync(FileAccessMode.ReadWrite);
                                    using (IOutputStream outStream = raStream.GetOutputStreamAt(0))
                                    {
                                        // Serialize the Session State.
                                        DataContractSerializer serializer = new DataContractSerializer(typeof(UserInfo));
                                        serializer.WriteObject(outStream.AsStreamForWrite(), CurrentUser);
                                        await outStream.FlushAsync();
                                    }
    Then In another page, I want to retrieve the file that has the user Info in above code by
    public async Task RestoreAsync()
    {    
        StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("UserInfo");    
        if (file == null) return ;
        IRandomAccessStream inStream = await file.OpenReadAsync();                      
        // Deserialize the Session State.
        DataContractSerializer serializer = new DataContractSerializer(typeof(UserInfo));
        CurrentUser = (UserInfo)serializer.ReadObject(inStream.AsStreamForRead());
        inStream.Dispose();
        this.DefaultViewModel["FirstName"] = CurrentUser.F_Name;
        this.DefaultViewModel["LastName"] = CurrentUser.L_Name;
    }


    Wednesday, December 18, 2013 10:01 PM