在.NET中独立存储应用数据

原文地址: http://www.dingos.cn/index.php?topic=2001.0

<!-- @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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜晚回家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值