WP7:删除IsolatedStorageFile

时间:2011-12-12 12:51:20

标签: windows-phone-7 delete-file isolatedstoragefile

我真的很感谢在WP7上从IsolatedStorage删除文件的一些帮助。我基本上是从网上下载文件,将其存储在独立存储中,然后将其上传到Dropbox中的我的下载文件夹。一旦我上传了它,我想从Isolated Storage中删除该文件,但在尝试时会出现异常错误。

这是我的代码:

    public void readCompleteCallback(Object sender, OpenReadCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            try
            {
                //string fileName = txtUrl.Text.Substring(txtUrl.Text.LastIndexOf("/") + 1).Trim();
                string fileName = searchBox.Text + fileExt;
                //string fileName = "DownloadedNZB.nzb";
                bool isSpaceAvailable = IsSpaceIsAvailable(e.Result.Length);

                if (isSpaceAvailable)
                {
                    // Save mp3 to Isolated Storage
                    using (var isfs = new IsolatedStorageFileStream(fileName,
                                        FileMode.CreateNew,
                                        IsolatedStorageFile.GetUserStoreForApplication()))
                    {
                        long fileLen = e.Result.Length;
                        byte[] b = new byte[fileLen];
                        var numberOfBytesRead = e.Result.Read(b, 0, b.Length);
                        isfs.Write(b, 0, numberOfBytesRead);
                        isfs.Flush();
                        isfs.Close();

                        isf = IsolatedStorageFile.GetUserStoreForApplication();
                        stream = isf.OpenFile(fileName, FileMode.Open);

                        MessageBox.Show("File downloaded successfully");

                        App.GlobalClient.UploadFileAsync("/Public/", fileName, stream, (response) =>
                            {
                                MessageBox.Show("Uploaded file to Dropbox OK.");
                            },
                         (error) =>
                         {
                             MessageBox.Show(error + "Cannot upload file to dropbox.");
                         });
                    }
                    //stream.Close();
                    isf.DeleteFile(searchBox.Text + fileExt);
                }
                else
                {
                    MessageBox.Show("Not enough to space available to download the file");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        else
        {
            MessageBox.Show(e.Error.Message);
        }

    }

我无法想象我哪里出错了,但如果有人能指出我正确的方向,我会很感激。

1 个答案:

答案 0 :(得分:0)

您正在尝试删除使用语句内的文件,其中文件尚未关闭

UPD:您的上传是异步的,因此您只能在文件完成后删除它。将您的代码放在MessageBox.Show附近(“将文件上载到Dropbox OK。”);