I considering writing an application in C#/.NET that will programmatically alter an XML file on a user's computer and I trying to determine if Silverlight (and which versions, in what modes) will wor开发者_开发技巧k for this task.
Here's the workflow:
- User selects file
- Application modifies file
- Application saves file, overwriting original file
Is this possible in Silverlight? If so, in which versions and under what conditions?
The silverlight runtime operates inside of a security 'sandbox'.
You can access files from the computers hard drive only via the Open file dialog. You can read and write from Isolated Storage using Silverlight 2 and up.
Coming in Silverlight 4 you will have the ability to access files from "special directories" such as My Documents, My Music, My Pictures etc. (http://weblogs.asp.net/nmarun/archive/2009/11/27/local-file-access-silverlight-4.aspx)
Some answer here concentrate on Isolated Storage. If no other application need access this data then perhaps Isolated Storage is the answer for you. This link is a blog describing how you Isolated Storage is used in a nutshell.
I suspect this possibly isn't what you want since you have the user select the file.
Certainly in SL3 you can have the user select the file and then you can read it.
However in order to save it again you would have to have to show a "Save File" dialog to the user, to aquire a File Stream you can write to.
In SL4 running OOB with elevated privs you can save files like this directly in the users folder such as My Documents.
Edit
Just to be absolutely clear, you cannot write to a file that was acquired using the OpenFileDialog
only read, you cannot read to a file that as acquired using SaveFileDialog
only Write.
SL3 enables access to an Isolated Storage area, SL4 opens up access even further for trusted applications allowing them to access files in MyDocs, MyPictures etc.
Silverlight applications may not change arbitrary files on the user's computer. However, they are able to store files locally using Isolated Storage:
In the .NET Framework, isolated storage is a storage mechanism that enables partially trusted applications to save data on the local machine without violating any security policies set on the computer. Isolated storage is around since the first version of the .NET Framework and is especially useful for downloaded, partially trusted components that are not usually given access to the standard I/O mechanisms. These same applications, though, are usually granted the right to use isolated storage. In this way, applications coming from potentially untrusted sources can still do some disk I/O, albeit in a controlled way.
You can read more about that in the following article:
Silverlight and Local Storage
The OS built-in File Open/Save dialog allows Silverlight to access/modify any file that the user specifies by returning a file stream to which the Silverlight app has access.
Other than that, the Silverlight app has access to its own isolated storage.
sl 4 has some capabilities
http://www.silverlight.net/learn/videos/silverlight-4-beta-videos/local-file-access/
but not general access to local file system
精彩评论