views:

558

answers:

6

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 work 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?

+1  A: 

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.

keithwarren7
could you expand on that and explain how that might work? Specifically, will I be able to accomplish my workflow in SL3 and under what conditions?
Ben McCormack
A: 

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

pm100
+3  A: 

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)

Dan
Will the read and write apply changes to the original file accessed through the Open File dialog box?
Ben McCormack
Also, to which version does this apply?
Ben McCormack
+1  A: 

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

0xA3
Thanks for the link "Silverlight and Local Storage." However, that applies to Version 2.0 of Silverlight. I'm wondering if more work has been done since then.
Ben McCormack
Apparently, Silverlight 4.0 will introduce a `SaveFileDialog` (http://msdn.microsoft.com/en-us/library/system.windows.controls.savefiledialog%28VS.95%29.aspx). However, this enables users to specify a local file name, applications are not allowed to choose the location themselves.
0xA3
+1  A: 

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.

Aaron
to which version(s) does this apply?
Ben McCormack
The File Dialog applies to V2.0 and later. I believe Isolated Storage applies to all versions.
Aaron
+2  A: 

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.

AnthonyWJones
Thanks for your detailed answer. When you say `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.`, is this possible in SL3 or does it require SL4?
Ben McCormack
Yes unless otherwise qualified I'm refering to the current version SL3. You can show a Save Dialog from an In-browser instance of SL3, the user chooses the file and as such has granted your code the right to write to the selected file as it sees fit. OpenFileDialog gives `FileInfo`(s) on which you can `OpenRead` but not `OpenWrite` whereas SaveFileDialog gives `FileInfo`(s) on which you can `OpenWrite` but cannot `OpenRead`.
AnthonyWJones
Ok. I think that makes sense. So if I wanted to modify a user's file, I would 1) ask the user for the file, 2) modify the file in code, then 3) ask the user where to save the file. If the user chooses to overwrite their original file, that's their choice, but the user still has to say where to save the file.
Ben McCormack
@Ben: That's exactly it.
AnthonyWJones