silverlight的独立存储
这个东西有点象Flash的Cookie,可以用来在客户端存储一些数据,我在官方文档上读到这个功能的第一反应就是:用它来做IM的客户端聊天记录存储太棒了,呵呵
这里把官方文档上的示例精减整理了一下,贴在这里纪念
先引用
using System.IO.IsolatedStorage;
using System.IO;
1try
2 {
3 using (var store = IsolatedStorageFile.GetUserStoreForApplication())
4 {
5
6 StringBuilder sb = new StringBuilder();
7 //创建主目录
8 store.CreateDirectory("MainDir1");
9
10
11 //在MainDir1下创建子目录SubDir1
12 string subdirectory1 = Path.Combine("MainDir1", "SubDir1");
13 store.CreateDirectory(subdirectory1);
14
15
16 //在SubDir1目录下创建文本文件demo.txt
17 IsolatedStorageFileStream subDirFile = store.CreateFile(Path.Combine(subdirectory1, "demo.txt"));
18 subDirFile.Close();
19
20 string filePath = Path.Combine(subdirectory1, "demo.txt");
21 if (store.FileExists(filePath))
22 {
23 try
24 {
25 using (StreamWriter sw = new StreamWriter(store.OpenFile(filePath, FileMode.Open, FileAccess.Write)))
26 {
27 sw.WriteLine("To do list:");
28 sw.WriteLine("1. Buy supplies.");
29 }
30 }
31 catch (IsolatedStorageException ex)
32 {
33 sb.AppendLine(ex.Message);
34 }
35 }
36
37 // 读取文件 MainDir1\SubDir1\demo.txt
38 try
39 {
40 using (StreamReader reader = new StreamReader(store.OpenFile(filePath,FileMode.Open, FileAccess.Read)))
41 {
42 string contents = reader.ReadToEnd();
43 sb.AppendLine(filePath + " contents:");
44 sb.AppendLine(contents);
45 }
46 }
47 catch (IsolatedStorageException ex)
48 {
49
50 sb.AppendLine(ex.Message);
51 }
52
53 //删除文件
54 try
55 {
56 if (store.FileExists(filePath))
57 {
58 store.DeleteFile(filePath);
59 }
60 }
61 catch (IsolatedStorageException ex)
62 {
63 sb.AppendLine(ex.Message);
64 }
65
66
67 //移除store
68 store.Remove();
69
70 sb.AppendLine("Store removed.");
71
72 txtParam.Text = sb.ToString();
73 }
74 }
75 catch (IsolatedStorageException ex)
76 {
77 txtParam.Text = ex.Message.ToString();
78
79 }
2 {
3 using (var store = IsolatedStorageFile.GetUserStoreForApplication())
4 {
5
6 StringBuilder sb = new StringBuilder();
7 //创建主目录
8 store.CreateDirectory("MainDir1");
9
10
11 //在MainDir1下创建子目录SubDir1
12 string subdirectory1 = Path.Combine("MainDir1", "SubDir1");
13 store.CreateDirectory(subdirectory1);
14
15
16 //在SubDir1目录下创建文本文件demo.txt
17 IsolatedStorageFileStream subDirFile = store.CreateFile(Path.Combine(subdirectory1, "demo.txt"));
18 subDirFile.Close();
19
20 string filePath = Path.Combine(subdirectory1, "demo.txt");
21 if (store.FileExists(filePath))
22 {
23 try
24 {
25 using (StreamWriter sw = new StreamWriter(store.OpenFile(filePath, FileMode.Open, FileAccess.Write)))
26 {
27 sw.WriteLine("To do list:");
28 sw.WriteLine("1. Buy supplies.");
29 }
30 }
31 catch (IsolatedStorageException ex)
32 {
33 sb.AppendLine(ex.Message);
34 }
35 }
36
37 // 读取文件 MainDir1\SubDir1\demo.txt
38 try
39 {
40 using (StreamReader reader = new StreamReader(store.OpenFile(filePath,FileMode.Open, FileAccess.Read)))
41 {
42 string contents = reader.ReadToEnd();
43 sb.AppendLine(filePath + " contents:");
44 sb.AppendLine(contents);
45 }
46 }
47 catch (IsolatedStorageException ex)
48 {
49
50 sb.AppendLine(ex.Message);
51 }
52
53 //删除文件
54 try
55 {
56 if (store.FileExists(filePath))
57 {
58 store.DeleteFile(filePath);
59 }
60 }
61 catch (IsolatedStorageException ex)
62 {
63 sb.AppendLine(ex.Message);
64 }
65
66
67 //移除store
68 store.Remove();
69
70 sb.AppendLine("Store removed.");
71
72 txtParam.Text = sb.ToString();
73 }
74 }
75 catch (IsolatedStorageException ex)
76 {
77 txtParam.Text = ex.Message.ToString();
78
79 }
存储区的默认大小是100K,如果感觉不够用了,可以用下面的代码向用户申请扩大:
1// Obtain an isolated store for an application.
2 try
3 {
4 using (var store = IsolatedStorageFile.GetUserStoreForApplication())
5 {
6 // Request 5MB more space in bytes.
7 Int64 spaceToAdd = 52428800;
8 Int64 curAvail = store.AvailableFreeSpace;
9
10 // If available space is less than
11 // what is requested, try to increase.
12 if (curAvail < spaceToAdd)
13 {
14
15 // Request more quota space.
16 if (!store.IncreaseQuotaTo(store.Quota + spaceToAdd))
17 {
18 // The user clicked NO to the
19 // host's prompt to approve the quota increase.
20 }
21 else
22 {
23 // The user clicked YES to the
24 // host's prompt to approve the quota increase.
25 }
26 }
27 }
28 }
29
30 catch (IsolatedStorageException)
31 {
32 // TODO: Handle that store could not be accessed.
33
34 }
2 try
3 {
4 using (var store = IsolatedStorageFile.GetUserStoreForApplication())
5 {
6 // Request 5MB more space in bytes.
7 Int64 spaceToAdd = 52428800;
8 Int64 curAvail = store.AvailableFreeSpace;
9
10 // If available space is less than
11 // what is requested, try to increase.
12 if (curAvail < spaceToAdd)
13 {
14
15 // Request more quota space.
16 if (!store.IncreaseQuotaTo(store.Quota + spaceToAdd))
17 {
18 // The user clicked NO to the
19 // host's prompt to approve the quota increase.
20 }
21 else
22 {
23 // The user clicked YES to the
24 // host's prompt to approve the quota increase.
25 }
26 }
27 }
28 }
29
30 catch (IsolatedStorageException)
31 {
32 // TODO: Handle that store could not be accessed.
33
34 }
想知道存储区的空间使用情况吗?
1// Obtain an isolated store for an application.
2 try
3 {
4 using (var store = IsolatedStorageFile.GetUserStoreForApplication())
5 {
6 string spaceUsed = (store.Quota - store.AvailableFreeSpace).ToString();
7 string spaceAvailable = store.AvailableFreeSpace.ToString();
8 string curQuota = store.Quota.ToString();
9 txtParam.Text =
10 String.Format("Quota: {0} bytes, Used: {1} bytes, Available: {2} bytes",
11 curQuota, spaceUsed, spaceAvailable);
12 }
13 }
14
15 catch (IsolatedStorageException)
16 {
17 txtParam.Text = "Unable to access store.";
18
19 }
2 try
3 {
4 using (var store = IsolatedStorageFile.GetUserStoreForApplication())
5 {
6 string spaceUsed = (store.Quota - store.AvailableFreeSpace).ToString();
7 string spaceAvailable = store.AvailableFreeSpace.ToString();
8 string curQuota = store.Quota.ToString();
9 txtParam.Text =
10 String.Format("Quota: {0} bytes, Used: {1} bytes, Available: {2} bytes",
11 curQuota, spaceUsed, spaceAvailable);
12 }
13 }
14
15 catch (IsolatedStorageException)
16 {
17 txtParam.Text = "Unable to access store.";
18
19 }
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。