我尝试从SkyDrive下载文件时出错

时间:2013-06-24 09:02:22

标签: c# windows-phone isolatedstorage onedrive

当我尝试从SkyDrive下载文件时,应用程序崩溃。 如果我再次打开应用程序,它就可以运行。

有什么不对?

这是xaml.cs:

namespace SDKMiniBrowserCS
{
    public partial class Address : PhoneApplicationPage
    {
        private LiveConnectClient client = null;
        private IsolatedStorageFileStream stream;
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        IsolatedStorageSettings inviato = IsolatedStorageSettings.ApplicationSettings;
    public Address()
    {
        InitializeComponent();
        Loaded += Customers_Loaded;



    }




    void Customers_Loaded(object sender, RoutedEventArgs e)
    {
        using (AddressBookDataContext dc = new AddressBookDataContext())
        {
            var itemSource = (from c in dc.Contacts select c).AsEnumerable();
            ContactListBox.ItemsSource = from c in itemSource orderby c.Name ascending select c;

        }
    }



    private void Edit_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        NavigationService.Navigate(
            new Uri(string.Format("/AddressDetail.xaml?Id={0}", (ContactListBox.SelectedItem as AddressUser).Id), UriKind.Relative));
    }

    private void Go_Fav(object sender, System.Windows.Input.GestureEventArgs e)
    {
        inviato["inviato"] = (ContactListBox.SelectedItem as AddressUser).Url;
        inviato["pannello"] = "1";
        inviato.Save();
        NavigationService.GoBack();
        }



    private void AddCustomerButton_Click(object sender, EventArgs e)
    {
        NavigationService.Navigate(new Uri("/AddressDetail.xaml", UriKind.Relative));
    }


    private void SignInButton_OnSessionChanged(object sender, Microsoft.Live.Controls.LiveConnectSessionChangedEventArgs e)
    {
        if (e.Status == LiveConnectSessionStatus.Connected)
        {
            client = new LiveConnectClient(e.Session);
            restore.IsEnabled = true;
            btnBackup.IsEnabled = true;
        }
    }

    private void OnBackupClicked(object sender, RoutedEventArgs e)
    {
        client.UploadCompleted += client_UploadCompleted;
        using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            stream = storage.OpenFile("3VSwipeBrowser.sdf", FileMode.Open);
            client.UploadAsync("me/skydrive/my_documents", "3VSwipeBrowser.sdf", stream, OverwriteOption.Overwrite, null);
        }
    }

    void client_UploadCompleted(object sender, LiveOperationCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            MessageBox.Show("Upload successfull!");
            stream.Close();
        }
    }

    private void OnRestoreClicked(object sender, RoutedEventArgs e)
    {
        string id = string.Empty;
        client.GetCompleted += (obj, args) =>
        {
            List<object> items = args.Result["data"] as List<object>;
            foreach (object item in items)
            {
                Dictionary<string, object> file = item as Dictionary<string, object>;
                if (file["name"].ToString() == "3VSwipeBrowser.sdf")
                {
                    id = file["id"].ToString();
                }
            }

            client.DownloadCompleted += (o, a) =>
            {
                Stream stream = a.Result;
                using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (
                        IsolatedStorageFileStream fileToSave = storage.OpenFile("3VSwipeBrowser.sdf", FileMode.Create,
                                                                                FileAccess.ReadWrite))
                    {
                        stream.CopyTo(fileToSave);
                        stream.Flush();
                        stream.Close();
                    }
                }
            };

            client.DownloadAsync(string.Format("{0}/content", id));
        };

        client.GetAsync("me/skydrive/my_documents/files");
        MessageBox.Show("Download successfull!");
        NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
    }

}}

错误在于行:

IsolatedStorageFileStream fileToSave = storage.OpenFile(“3VSwipeBrowser.sdf”,FileMode.Create,FileAccess.ReadWrite))

由于

1 个答案:

答案 0 :(得分:0)

FileAccess.ReadWrite))必须是FileAccess.ReadWrite)我认为