How to Access the Local File System in Silverlight - silverlight

As silverlight do not have access to local system files, we cannot perform any action on such files like to copy, move, delete, open ect.
In my case i want to get the path of file(s) located on local System, but getting some security exception. Please suggest some possible way to get access to the Local File System.
Thanks in Advance!!

To read a file from the filesystem in Silverlight, you can't use the pathname of the file. If you're not running out-of-browser with elevated trust, then you're prohibited from accessing this information for security reasons.
However, the FileInfo objects obtained from an OpenFileDialog have a OpenRead() method, which returns a Stream that reads the data from that file. From reading your comment, this appears to be all you need, so I suspect that you don't actually need the paths of files on the user's local system at all.
I gave a similar answer to another question about uploading files in Silverlight a few months ago.

If you're not running out-of-browser with elevated trust, then you're prohibited from accessing this information for security reasons.
http://social.msdn.microsoft.com/Forums/silverlight/en-US/da778e7c-e790-4bb2-92b8-cea815789d22/silverlight-4-local-filesystem-access?forum=silverlightnet

These tasks are not possible in Silverlight 4 without using Out-of-Browser with Elevated Trust.
In Silverlight 5 its possible after jumping through a few hoops to get an inbrowser app to have elevated trust.

Related

Same File operation not permitted. Access to path denied, Silverlight 5.0

I am working with a silverlight application, File.WriteAllText is giving File operation not permitted when trying to write to local desktop. Access to path exception.
This code always works on local desktop but, is giving error when code is deployed on server.
Silverlight does not have the access priveleges like u expect. If you want to add a file to your Silverlight Application at runtime, you will need to have Silverlight running Out of the Browser with Elevated priveleges. There are certain limitations to this too. You can only access files in Special Folders like My Documents, Pictures, Music etc. For more info, see this and this

Path.GetTempFileName causes Access to path '' is denied.in Silverlight

I am have a problem with Path.GetTempFileName with running a Silverlight application. If I install my application to run Out-Of-browser it runs ok. It only fails when I run it locally i.e. In-Browser.
The error is
File operation not permitted. Access to path '' is denied.
I cannot debug my application because of this issue. Can anyone suggest a work around?
The problem is probably no access to where ever Path.GetTempPath is pointing to.
If you have access to say My Documents, you could combine that with Path.GetRandomFileName, or if it's not happening too often, a simple time stamp.
Maybe you could use isolated storage. You could ask to make it a trusted application which would gain you access to the file system but doesn't work in say windows phone.
The GetTempFileName method tries to access the temporary folder which is typically on the C:\ (or whichever letter the main drive has been assigned). For security reasons, in-browser applications aren't allowed local access and that's likely why you're getting an error.

Is there a way to get available disk space in Silverlight?

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

how to open a file in SL 4.0 without OpenFileDialog?

is there an easy way to open a file in read mode from a local drive in Silverlight 4.0? I think there is OpenFileDialog, but I dont want to show any dialog to use but silently read some file on local drive
The only way to do this is with the Silverlight app running Out-of-browser with elevated trust. Even then the folders you can access are restricted to the users MyDocuments, MyMusic, MyPictures and MyVideos as defined in teh Environemnt.SpecialFolder enum.
For security reason any other access must be explicitly granted by the user via the OpenFileDialog.
While there is no way to do this (outside of going out-of-browser to restricted folders) in Silverlight 4, there are options to do this in Silverlight 5.

"Out of browser" web application running at Start-Up?

I've become familiar with the new concept of "out of browser" web applications, supported in the recent Silverlight, JavaFX, Adobe AIR etc.
Listening recently to a podcast on the subject by Scott Hanselman, I've become aware that one of the purposes behind these new architectures is to allow for "desktop-application-feel". Also, I understand some (or all) of these allow for some offline access to a sandbox of resources. This really sounds as if these frameworks could be an alternative to "real" desktop applications, as long as the application does not require messing with the user's machine (i.e. access to peripherals, certain file IO, etc).
I have a very specific question. My application needs to run at start-up. Is it possible to do so using such a framework without requiring the user to download and run a certain executable?
For example, I could always direct the user to download a small EXE that will put a .lnk file in the start-up directory, but I want to avoid such a patch.
To summarize: is it possible to have an out-of-browser web application setup itself to run at start-up without requiring file download?
To further clarify, this question does not come from an "evil" place, but rather from trying to decide whether "out-of-browser" frameworks are indeed a proper alternative to a desktop application, for my specific requirements.
The BkMark example here shows how to start an application on startup using Adobe Air. So, yes it is possible.
So, here's the deal: web apps in general will have a security context around them, and by default won't have access to write to the filesystem (outside of a temp files), access the registry, etc.
One way is, as you said, have the user run something or configure it so the lnk is executed on startup.
Another way, and I think, more in line of what you want, is that the user can run the program himself, click some button in the application, and it's configured.
I know with Java you could do this, but the user has to allow full access to their system, because your app would need to change System configuration. Then you could just configure it (by writing a lnk to your WebStart JNLP in the Startup folder)
For Internet Exploder, Javascript apps do have write access to the disk.
For other (better-secured) browsers you will either need to have a download, or Adobe AIR.
Assuming you are building for Windows, launching an executable at startup can be done several ways.
For user session startup, you can achieve this either by putting a lnk file in the appropriate folder, or with a registry entry. For operating system startup, you can achieve this with a registry entry. There are several permutations:
run application once on boot (UI not allowed)
run application every boot (UI not allowed)
start service every boot according to policy set in registry
run application once on user session start
run application every user session
Since an out of browser application has UI I expect you mean run application every user session and in this case you may as well put an LNK file in the user's startup folder.
I just created a shortcut for an SL4 OOB application, and this was the Target of the shortcut:
"C:\Program Files (x86)\Microsoft Silverlight\sllauncher.exe" 2635882436.localhost
A search of my disk revealed that location 2635882436.localhost is a folder.
C:\Users\<mylogin>\AppData\LocalLow\Microsoft\Silverlight\OutOfBrowser\2635882436.localhost
I rather doubt an OOB app of any type could place a shortcut in the Startup folder unless you somehow obtained Full Trust.

Resources