Posted by

Silverlight File Dialog Save

File Access, Dialogs, Stream, Isolated Storages Abhishek Shukla. In Silverlight there are 3 main ways of accessing files Open. File. Dialog and Save. File. Dialog This is the most flexible of all the file access options as this can be used by any Silverlight application running at any permission level. The user can select any file as long as the user has permissions for the same. The user will have to select the file to read and while saving the user has to specify the filename to override or save as. The code has no permission to know the location of the file and unless the code has elevated permissions the path of the file cannot be known rather only the filename is available. The ability to know the exact path or location is considered to be a security risk by Silverlight. Also these File dialogs are also not allowed until the code for these dialogs is being run in an event handler for user input. File. Stream, Stream. Writer The second way is to use the different classes in the System. IO namespace. Silverlight offers classes like File, File. Info, Directory, Directory. Info, File. Stream, Stream. Writer, etc. In these operations user is not involved. This mode of operation is available only for the trusted application. Hi I implement a web application in Silverlight 5,I have a button including following code for save a file in client side, it works fine in all browsers in local. A dialog appear to the user to either open it or save. You cant use SaveFileDialog in ASP. NET because it. The SaveFileDialog will help you select a location and a filename when you wish to save a file. It works and looks much like the OpenFileDialog which we used in the. New Controls. Silverlight is packed with over 60 high. Save As Dialog' title='Save As Dialog' />Html Save File DialogVb Save File Dialog ExampleNot all Silverlight applications can use unless the application is running under elevated privileges. Isolated Storage A Silverlight application can read or write files to the Isolated Storage without needing the user to be involved. Download Naruto Shippuden Episode 91 more. The files can be read or written. The only catch is that the files are saved in the private storage which is under the user directory and the path is not available. So you can just use it as a private store of files where you can access the files. There is no access available for the user files and neither the user can look at the files that are saved unless the user looks into the most of the hidden folder and files under their directory. So the main purpose of Isolated Storage is to provide the application the fast and easy access of files store without user input or elevated trust. But this storage is not reliable as the user can always delete the files. Lets have a look at each of these File Access methods in detail Save. File. Dialog. The Save. File. Dialog uses the same file save dialog box supplied by the operating system. To use it we need to create an instance of the Save. File. Dialog and call the Show. Dialog method. It returns true or false depending on whether the user selected a file or not. The return type of Show. Dialog method is nullable bool so we need to compare the return value of the Show. Dialog method with true to proceed further with the file selected. However the Show. Dialog never returns null but this is the way it is designed. As you can see in the image below once we verify that the user has a selected a filename we can open the file. We can get the filename of the file from the Save. File. Name property but the path is not available. So the only way write data is to write the string returned by the dialog. Silverlight 4 Save File Dialog. From the perspective of. Net this is only a string which we can put in a Strem. Writer to write text into it. If we want to write binary data into it then we can use the Stream Object directly. We can browse to the selected folder and have a look at the saved file. Open. File. Dialog. The Open. File. Dialog is similar to the Save. File. Dialog. The major difference being  that File. Open. Dialog offers a property called multi select which when set to true the user can choose multiple files. So the Open. File. Dialog being more complex does not offer a Open. File method. So depending on whether the Open. File. Dialog is in single file or multi file mode we need to use the File or Files property respectively. So as we see in the image below we call File. Open. Read as we are dealing with single file. In the multi select mode we would use the Files property which would return a collection of File. Info object. Silverlight 4 Open File Dialog. File. Stream. The usage of File. Stream is quite similar to the regular. NET File access using File. Stream. However in Silverlight only application running in elevated trust are allowed to use this technique which is available only in Out of Browser mode. Another restriction in Silverlight is that the files only in specific folder location are available using File. Stream. The accessible files are the ones which are in the Users Documents, Music, Pictures and Videos. The reason is because Silverlight runs on multiple platforms and the file system on these platforms might be different and location of these folders might be different on different platforms. We need to use the Enviorment. Get. Folder. Path method to access these folders. We can also inspect the directory structure beneath these folders using the Directory and Directory. Info class in System. IO. Isolated Storage. It provides storage associated with the logged in user. The API presents data from the stream class from the System. IO namespace. This storage can be used to store either text or binary data. Its called as Isolated as the storage is partitioned and the Silverlight application have access only to specific parts. Firstly, the storage is partitioned as per the user so that the apps do not have access to the storage space allocated to the other user of the machine. Secondly, the storage is partitioned as per the site and optionally as per the application in the site as well. The partition of the isolated storage depends on the xap files that the apps access. The isolated storage is not dependent on the hosting page. If the multiple pages of an application access the same xap then they will share the same isolated storage. This works for a multi site website as well. So if multiple sites download the same xap file then the Silverlight supplication will have access to the same Silverlight application isolated store. The Silverlight application provides 1. MB of space by default for each user. If the application has more space requirement then it can ask the user for it. Isolated storage is available across browsers on the same machine similar to sharing the cookies across all bowsers, it takes the cross platform support to whole new level. How To Read A Column From Csv File In Python. You might be surprised to know that Silverlight is not the pioneer of isolated storage. It was introduced in Widows forms to store data from the web in the partial trust scenarios. Although the method of access is different and there is no way to access the full. Net framework featured Isolated storage. Now lets see how to Write to the Isolated storage. Add the following to your Silverlight application. Obtaining the isolated storage from the userusing Isolated. Storage. File iso. Store Isolated. Storage. File. Get. User. Store. For. ApplicationCreate new fileusing Isolated. Storage. File. Stream iso. Stream new Isolated. Storage. File. StreamMy. Data. txt,File. Mode. Create, iso. Storewrite some content to the fileusing Stream. Writer writer new Stream. Writeriso. StreamWriting line to the filewriter. Write. LineIsolated storage is a cool feature catch As you would see that we need to begin by asking the user specific store for the application. We can also get the user store shared by all the application on the site by calling Get. USer. Store. For. Site. The method returns the directory. Then we create an Isolated. Storage. File. Stream and the constructor requires the Isolated. Storgae. File as one of the inputs.