Silverlight independent file storage

Source: Internet
Author: User

If you know more about the content, it is not recommended for the SL fans;

Intended audience: This article is suitable for users who have basic knowledge about SL and learn about SL. This article does not have any difficult code. You can master the method of independent file storage.

Independent files are stored in your project;

1. What is SL independent file storage?

Silverlight Code itself cannot go to the operating system. To put it bluntly, it cannot do some CRUD (create, delete, and ect files in your system) for your system files );

If such an operation is allowed, you must enable the Security Mode of IE. However, you must use the data storage function in the Silverlight application, SL calls Isolated Storage (independent Storage)

This storage method provides a disk storage space, which is a virtual file system that can store a small amount of data. By default, it can only store 1 MB of files; the user may be able

Reasonable allocation of space;

 

2. Operations on independent files;

We can use the IsolateStorageFiel class in the namespace named Syste. IO. We will get the IsolatedStorageFile. GetUserStoreForApplicttion for the current application of an IslatedStorageFile object, as shown below:

IsolatedStorageFile store = IsolatedStorageFile. GetUserStoreForApplication ();

Common IsolatedStorageFile methods are as follows:

Method Name Remarks
CreateDirectory () Create a new independent storage folder
DeleteDirectory () Delete an independent storage folder
CreateFile () Create a file
DeleteFile () Delete an object
Remove () Remove all files and folders
GetFileNames () Get the file name set
GetDirectoryNames () Get the folder name set
OpenFile () Open a file

 

 

In SL, the read and write operations of independent files are basically the same as those of the file data we use. The StreamWrite and StreamRead methods are used;

Create a file named cao.txt and write it to the current time. The file is written as follows:

// Text? T? Write '? Try {using (IsolatedStorageFile store = IsolatedStorageFile. getUserStoreForApplication () {using (IsolatedStorageFileStream stream = store. createFile ("cao.txt") {StreamWriter writer = new StreamWriter (stream); writer. write (DateTime. now); writer. close ();} MessageBox. show ("text? T write '? Cheng é Gong | ") ;}} catch (Exception err) {MessageBox. Show (err. Message. ToString ());}

How can we read the file information? The Code is as follows:

Try {using (IsolatedStorageFile store = IsolatedStorageFile. getUserStoreForApplication () {using (IsolatedStorageFileStream stream = store. openFile ("date.txt", FileMode. open) {StreamReader reader = new StreamReader (stream); lblData. text = reader. readLine (); reader. close () ;}} catch (Exception err) {MessageBox. show (err. message. toString ();} It is not difficult to find its storage location through debugging when reading the file,

C: \ Users \ matthew \ AppData \ LocalLow \ Microsoft \ Silverlight \ is \ sid3dsxe. u1y \ lstesiyg. ezx
\ S \ atkj2fb5vjnabwjsx2nfj3htrsq1ku1h \ f \ cao.txt. files stored in different operating systems cannot be found through debugging;

3. self-understanding

All the file read and write operations are acceptable, so we can think of ASP. . NET file, in ASP. NET also has a COOKIE, which is also written on our client,

Some user information may be saved. I am not using ASP here. The COOKIE in NET has been explained too much. To put it more understandable, the storage of independent files in Silverlight is a bit different from that of cookies.

At present, I only have so many studies on it. It can also solve the problem of saving the user's login information in the project. There is also a security issue here, you can solve the problem according to your own requirements;

 

This article is the author's reference to Apress Pro. Silverligth 2, Apress Pro. Silverligth 3, relevant documents, and the MSDN official documentation, to write their own study notes, this article is for reference only;

Due to your limited understanding ability, it is inevitable that you may not understand it correctly. I hope you can correct it;

Author: good man (learning together and making progress together)
Source: http://www.cnblogs.com/caodaiming
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.