<!-- @font-face {font-family:"cambria math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:-1610611985 1107304683 0 0 159 0;} @font-face {font-family:华文中宋; panose-1:2 1 6 0 4 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:647 135200768 16 0 262303 0;} @font-face {font-family:"@华文中宋"; panose-1:2 1 6 0 4 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:647 135200768 16 0 262303 0;} p.msonormal, li.msonormal, div.msonormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"courier new"; mso-fareast-font-family:华文中宋; mso-bidi-font-family:"times new roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} .msochpdefault {mso-style-type:export-on<wbr>ly; mso-default-props:yes; mso-ascii-font-family:"courier new"; mso-fareast-font-family:华文中宋; mso-hansi-font-family:"courier new"; mso-bidi-font-family:"times new roman"; mso-bidi-theme-font:minor-bidi;} @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.section1 {page:section1;} --> -->-->
许多程序与使用配置文件保存应用程序配置数据。但是这样有一个缺点,它是只 读的。不能使用 .NET 中的 ConfigurationSettings 类来修改应用配置文件中的数据。在早期,使用 .ini 文件或注册表保存应用程序的具体 数据。
当然我们可以使用普通的文件保存应用数据。但现在文件是如何保护数据。
.NET 引入了独立存储区概念。独立存储区就像一个虚拟文件夹。用户不需要知道文件存储的确切的位置。你 所作的就是告诉 .NET Framework 在独立存储区存储你的文件。对于不同的操作系统独立存储区的物理位置是不同的。在你的应用程序中 简单的使用 .NET 中的类创建和访问文件,不需要担心文件存储的物理位置。
下面的代码演示对独立存储区的基本操作 – 创建、写、读。
在 你的文件顶部需要包括下面的指令:
using System.IO;
using System.IO.IsolatedStorage;
using System.Diagnostics;
代码示例:
const string ISOLATED_FILE_NAME = "MyIsolatedFile.txt";
//-------------------------------------------------------
// 检 查在独立存储区是否已经存在文件
//-------------------------------------------------------
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore
(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
string[] fileNames = isoStore.GetFileNames( ISOLATED_FILE_NAME );
foreach ( string file in fileNames ){
if ( file == ISOLATED_FILE_NAME ){
Debug.WriteLine("The file already exists!");
}
}
//-------------------------------------------------------
// 在 独立存储区写一些文本到文件
//-------------------------------------------------------
IsolatedStorageFileStream oStream =
new IsolatedStorageFileStream( ISOLATED_FILE_NAME, FileMode.Create,
isoStore );
StreamWriter writer = new StreamWriter( oStream );
writer.WriteLine( "This is my first line in the isolated storage file." );
writer.WriteLine( "This is second line." );
writer.Close();
//-------------------------------------------------------
// 从 独立存储区读文件中的所有行
//-------------------------------------------------------
IsolatedStorageFileStream iStream =
new IsolatedStorageFileStream( ISOLATED_FILE_NAME, FileMode.Open,
isoStore );
StreamReader reader = new StreamReader( iStream );
String line;
while ( (line = reader.ReadLine()) != null ){
Debug.WriteLine( line );
}
reader.Close();