3

I'm currently trying to determine the available disk space from my Silverlight application.

My app can download a lot of data (video files) and obviously, this can fail if the disk space isn't there. I know I can catch exceptions on writes, but that will not lead to a very user-friendly experience, because the information will come too late and cause useless waits.

In regular .NET, I would be using DriveInfo (see How do I retrieve disk information in C#? for an example), but that class isn't present as of Silverlight 5, even in elevated trust mode.

So, is there a way to determine the available space on a drive in Silverlight?


Update:

  • I'm fine with requiring Elevated Priviledges for my application.
  • I'm also fine with Silverlight 5 only solutions.
  • I need something that works on both Windows and Mac OS, so PInvoke/COM interop is not an option.
Community
  • 1
  • 1
jv42
  • 8,521
  • 5
  • 40
  • 64

2 Answers2

1

There has been filebrowser demos out there written in Silverlight but they would run with elevated trust.

That means that you would have to make the user immediately suspicious of your application when they first run it.

It's probably a better user experience to just have a well worded error message for when the user runs out of space.

Another option would be to try an increase the isolated storage quota by the size of the biggest video available.

http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.increasequotato(v=vs.95).aspx

Then when that fails just let the user know that no more space can be allocated for the app had that he may need to delete older videos.

jv42
  • 8,521
  • 5
  • 40
  • 64
Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84
  • I'm OK with using elevated trust, I will clarify that in my question. – jv42 Dec 21 '11 at 11:04
  • IsolatedStorage quota is (sadly) not related to real disk space available, you can request any quota you wish, it's just used to request permission from the user to write that amount. – jv42 Dec 21 '11 at 11:05
  • 1
    you can do this http://forums.silverlight.net/t/146323.aspx/1 but it will be windows only. finding disk space will be windows only. – Joseph Le Brech Dec 21 '11 at 11:08
  • I would like to avoid COM interop, I need it to work on Mac too. – jv42 Dec 21 '11 at 11:13
  • Isn't running out of disk space a very rare occurrence these days? I agree that you should just fail if you run into this problem, instead of always checking for it. Best case, do the check on Windows with pinvoke, and fail if you get an error on Mac – RobSiklos Dec 21 '11 at 15:23
  • @RobSiklos no, it's not that rare in my case, especially considering that IsolatedStorage is *always* located on the system drive. For people with partitions, and a small system partition, downloading videos is going to fill up the partition very quickly. I'm trying to implement a 'save anywhere' policy with SL5 to mitigate precisely that. – jv42 Dec 21 '11 at 15:30
  • This is a longshot, but if you are able to run system commands on MAC when in full trust, it should be a simple matter of running the "df" command and parsing the output. There might even be a special file on the filesystem somewhere that contains this information. – RobSiklos Dec 21 '11 at 15:44
  • if you can run a system command you could also ship a separate disk space utility with windows for it to run. – Joseph Le Brech Dec 21 '11 at 15:46
  • Thanks, I'll consider it if nothing better comes up. Please note that I'm working on public projects, not LOB, so getting the user to install anything is a pain. – jv42 Dec 21 '11 at 15:57
  • so you should have a large error message telling the user to contact their administrator, they have probably run up their hard drives with personal data anyway. – Joseph Le Brech Dec 21 '11 at 16:36
0

I'm adding my answer here to sum up my discoveries:

TL/DR: there is no easy way to get available disk space in Silverlight 5 that is cross-platform (Windows/Mac OS).

  • You can't get available disk space with standard Silverlight calls. DriveInfo is missing from Silverlight 5, elevated privileges don't come into account here.
  • Quota is useless for that kind of issue, it doesn't take into account available disk space.
  • There are workarounds for Windows only, requiring elevated trust, using P/Invoke into Win32.
  • For a detailed support of filesystem, see this article: http://www.codeproject.com/KB/silverlight/FileExplorerInSilverlight.aspx
  • Fall-back is to check for exceptions when writing files and present the user with a message at the time of writing. People have also suggested pre-writing the file when the download start to ensure sufficient disk space.
jv42
  • 8,521
  • 5
  • 40
  • 64