views:

468

answers:

4

The following code throws an exception...

private void EnsureDiskSpace()
{
    using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForSite())
    {
        const long NEEDED = 1024 * 1024 * 100;

        if (file.AvailableFreeSpace < NEEDED)
        {
            if (!file.IncreaseQuotaTo(NEEDED))
            {
                throw new Exception();
            }
        }
    }
}

But this code does not (it displays the silverlight "increase quota" dialog)...

private void EnsureDiskSpace()
{
    using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForSite())
    {
        const long NEEDED = 1024 * 1024 * 100;

        if (file.Quota < NEEDED)
        {
            if (!file.IncreaseQuotaTo(NEEDED))
            {
                throw new Exception();
            }
        }
    }
}

The only difference in the code is that the first one checks file.AvailableFreeSpace and the second checks file.Quota.

Are you not allowed to check the available space before requesting more? It seems like I've seen a few examples on the web that test the available space first. Is this no longer supported in SL3? My application allows users to download files from a server and store them locally. I'd really like to increase the quota by 10% whenever the user runs out of sapce. Is this possible?

A: 

I believe that there were some changes to the behavior in Silverlight 3, but not having worked directly on these features, I'm not completely sure.

I did take a look at this MSDN page on the feature and the recommended approach is definitely the first example you have; they're suggesting:

  1. Get the user store
  2. Check the AvailableFreeSpace property on the store
  3. If needed, call IncreaseQuotaTo

It isn't ideal, since you can't implement your own growth algorithm (grow by 10%, etc.), but you should be able to at least unblock your scenario using the AvailableFreeSpace property, like you say.

I believe reading the amount of total space available (the Quota) to the user store could be in theory an issue, imagine a "rogue" control or app that simply wants to fill every last byte it can in the isolated storage space, forcing the user eventually to request more space, even when not available.

Jeff Wilcox
A: 

It turns out that both code blocks work... unless you set a break point. For that matter, both code blocks fail if you do set a break point.

herbrandson
+1  A: 

There is a subtle bug in your first example.

There may not be enough free space to add your new storage, triggering the request - but the amount you're asking for may be less than the existing quota. This throws the exception and doesn't show the dialog.

The correct line would be

file.IncreaseQuotaTo(file.Quota + NEEDED);
Dave Evans
+3  A: 

I had the same issue. The solution for me was something written in the help files. The increase of disk quota must be initiated from a user interaction such as a button click event. I was requesting increased disk quota from an asynchronous WCF call. By moving the space increase request to a button click the code worked.

In my case, if the WCF detected there was not enough space, the silverlight app informed the user they needed to increase space by clicking a button. When the button was clicked, and the space was increased, I called the WCF service again knowing I now had more space. Not as good a user experience, but it got me past this issue.

Andrew

related questions