IsolatedStorageSettings
因为它所提供的功能很有限,而隔离存储所提供的是一整套本地存储的方案,包括目录,文件管理等方面.
本文将会继续上文中的内容,通过一个本地文件系统管理的DEMO来演示一下如果使用下面两个隔离存储类: IsolatedStoragefile (返回一个包括路径和文件的虚拟区域,用于管理目录,文件等)
IsolatedStoragefileStream (以流的方式读写指定路径的文件)
首先请大家看一下这个DEMO的演示效果:
我们可以在这里进行目录的选择,文件的选择,以及在相应的目录下添加,编辑,删除工作.而实现这些功能
都用到了上面所说的两个类. 比如IsolatedStoragefile就提供了如下几个常用的方法:
CreateDirectory() //创建路径 Creates a directory in the isolated storage scope.
Createfile() //创建文件 Creates a file in the isolated store.
DeleteDirectory()//删除路径 Deletes a directory in the isolated storage scope.
Deletefile() //删除文件 GetDirectorynames //获取指定路径下的目录列表
Getfilenames //获取指定路径下的文件列表 以及相应的路径文件检查方法:
DirectoryExists()
fileExists() 而另外的一个类是IsolatedStoragefileStream,它主要是对给定路径下的文件进行流读写 *** 作,我们可以
使用如下方法将其绑定了一个读或写的流对象上: IsolatedStoragefileStream subDraftfile = store.Createfile(filePath);
using (StreamWriter writer = new StreamWriter(subDraftfile))
{
//
}
好的,下面是这个DEMO的page.xaml文件代码: < GrID x:name ="LayoutRoot" Background ="AntiqueWhite" >
< GrID.ColumnDeFinitions >
< ColumnDeFinition WIDth ="0.307*" />
< ColumnDeFinition WIDth ="0.176*" />
< ColumnDeFinition WIDth ="0.517*" />
</ GrID.ColumnDeFinitions >
< GrID.RowDeFinitions >
< RowDeFinition Height ="40" />
< RowDeFinition Height ="40" />
< RowDeFinition Height ="231" />
< RowDeFinition Height ="*" />
</ GrID.RowDeFinitions >
< StackPanel OrIEntation ="Vertical" GrID.Row ="0" GrID.Column ="0" GrID.rowspan ="4" Background ="AliceBlue" >
< TextBlock GrID.Row ="0" GrID.Column ="1" HorizontalAlignment ="Stretch" margin ="5,5,5" >
< Run Text ="路径:" />
</ TextBlock >
< ListBox x:name ="DirectoryList" SelectionChanged ="DirectoryList_SelectionChanged" margin ="5,5" />
< TextBlock GrID.Row ="0" GrID.Column ="1" HorizontalAlignment ="Stretch" margin ="5,5" >
< Run Text ="文件列表:" />
</ TextBlock >
< ListBox x:name ="fileList" SelectionChanged ="fileList_SelectionChanged" margin ="5,5" Height ="150" />
</ StackPanel >
< TextBlock GrID.Row ="0" GrID.Column ="1" HorizontalAlignment ="Stretch" margin ="5,5" >
< Run Text ="收件人:" />
</ TextBlock >
< TextBox x:name ="RecipIEnt" WIDth ="200" GrID.Row ="0" GrID.Column ="2" margin ="5,5" HorizontalAlignment ="left" d:LayoutOverrIDes ="GrIDBox" />
< TextBlock GrID.Row ="1" GrID.Column ="1" HorizontalAlignment ="Stretch" margin ="5,5" >
< Run Text ="主 题:" />
</ TextBlock >
< TextBox x:name ="Subject" WIDth ="200" GrID.Row ="1" GrID.Column ="2" margin ="5,5" HorizontalAlignment ="left" d:LayoutOverrIDes ="GrIDBox" />
< TextBlock GrID.Row ="2" GrID.Column ="1" HorizontalAlignment ="Stretch" margin ="5,5" >
< Run Text ="内 容:" />
</ TextBlock >
< TextBox x:name ="Body" textwrapPing ="Wrap" GrID.Row ="2" GrID.Column ="2" margin ="5,5" HorizontalAlignment ="Stretch" GrID.rowspan ="1" d:LayoutOverrIDes ="GrIDBox" RendertransformOrigin ="0.5,0.5" >
</ TextBox >
< StackPanel GrID.Row ="3" OrIEntation ="Horizontal" GrID.Column ="1" GrID.ColumnSpan ="2" margin ="0,8" >
< button x:name ="AddToDraft" margin ="5,5" Content =" 添加草稿 " Click ="AddToDraft_Click" />
< button x:name ="SavetoDraft" margin ="5,5" Content =" 编辑草稿 " Click ="SavetoDraft_Click" />
< button x:name ="DeleteDraft" margin ="5,5" Content =" 删除当前草稿 " Click ="DeleteDraft_Click" />
</ StackPanel >
</ GrID >
而相应的page.xaml.cs文件如下(相关内容见注释): public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
this .Loaded += new RoutedEventHandler(Page_Loaded);
}
voID Page_Loaded( object sender, RoutedEventArgs e)
{
using (var store = System.IO.IsolatedStorage.IsolatedStoragefile.GetUserStoreForApplication())
{
// 如果本地没有文件夹,则初始化本地文件夹信息
if ( ! store.DirectoryExists( " 本地文件夹 " ))
{
// 在当前根目录下创建三个目录
store.CreateDirectory( " 本地文件夹 " );
// 可使用下面方面在指定目录下创建子目录
store.CreateDirectory(System.IO.Path.Combine( " 本地文件夹 " , " 收件箱 " ));
store.CreateDirectory(System.IO.Path.Combine( " 本地文件夹 " , " 发件箱 " ));
store.CreateDirectory(System.IO.Path.Combine( " 本地文件夹 " , " 草稿箱 " ));
}
// 加载文件夹信息到控件
foreach ( string directory in GetAllDirectorIEs( " * " , store))
{
DirectoryList.Items.Add(directory);
}
}
}
/// <summary>
/// 添加数据到草稿箱
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private voID AddToDraft_Click( object sender, RoutedEventArgs e)
{
using (var store = System.IO.IsolatedStorage.IsolatedStoragefile.GetUserStoreForApplication())
{
string filePath = DirectoryList.SelectedItem.ToString();
if (store.DirectoryExists(filePath))
{
filePath = System.IO.Path.Combine(filePath, Subject.Text + " .txt " );
if ( ! store.fileExists(filePath))
{
IsolatedStoragefileStream subDraftfile = store.Createfile(filePath);
using (StreamWriter writer = new StreamWriter(subDraftfile))
{
writer.Writeline( " RecipIEnt: " + RecipIEnt.Text);
writer.Writeline( " Subject: " + Subject.Text);
writer.Writeline( " Body: " + Body.Text);
}
subDraftfile.Close();
}
else
{
HTMLPage.Window.Alert( " 文件已存在,请修改主题后再进行保存 " );
}
}
}
}
/// <summary>
/// 保存草稿箱的数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private voID SavetoDraft_Click( object sender, RoutedEventArgs e)
{
using (var store = System.IO.IsolatedStorage.IsolatedStoragefile.GetUserStoreForApplication())
{
string filePath = DirectoryList.SelectedItem.ToString() + " / " + fileList.SelectedItem.ToString();
if (store.fileExists(filePath))
{
store.Deletefile(filePath);
IsolatedStoragefileStream subDraftfile = store.Createfile(filePath);
using (StreamWriter writer = new StreamWriter(subDraftfile))
{
writer.Writeline( " RecipIEnt: " + RecipIEnt.Text);
writer.Writeline( " Subject: " + Subject.Text);
writer.Writeline( " Body: " + Body.Text);
}
subDraftfile.Close();
}
else
{
HTMLPage.Window.Alert( " 要修改的当前文件不在本地隔离存储!!! " );
}
}
}
/// <summary>
/// 删除指定草稿
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private voID DeleteDraft_Click( object sender, RoutedEventArgs e)
{
if (fileList.SelectedItem != null )
{
using (var store = System.IO.IsolatedStorage.IsolatedStoragefile.GetUserStoreForApplication())
{
string filePath = DirectoryList.SelectedItem.ToString() + " / " + fileList.SelectedItem.ToString();
if (store.fileExists(filePath))
{
if (HTMLPage.Window.Confirm( " 是否删除文件: " + filePath))
{
store.Deletefile(filePath);
}
}
}
}
}
/// <summary>
/// 分割字符串
/// </summary>
public static string [] SplitString( string strContent, string strSplit)
{
if (strContent != null && strContent != "" )
{
if (strContent.IndexOf(strSplit) < 0 )
{
string [] tmp = { strContent };
return tmp;
}
return Regex.Split(strContent, Regex.Escape(strSplit), RegexOptions.IgnoreCase);
}
else
{
return new string [ 0 ] { };
}
}
/// <summary>
/// 下面代码从网上获得,用于递归指定路径下的所有目录
/// </summary>
/// <param name="pattern"></param>
/// <param name="storefile"></param>
/// <returns></returns>
public string [] GetAllDirectorIEs( string pattern, System.IO.IsolatedStorage.IsolatedStoragefile storefile)
{
// Get the root of the search string.
string root = System.IO.Path.GetDirectoryname(pattern);
if (root != "" ) root += " / " ;
// RetrIEve directorIEs.
string [] directorIEs;
directorIEs = storefile.GetDirectorynames(pattern);
List < string > directoryList = new List < string > (directorIEs);
// RetrIEve subdirectorIEs of matches.
for ( int i = 0 , max = directorIEs.Length; i < max; i ++ )
{
string directory = directoryList[i] + " / " ;
string [] more = GetAllDirectorIEs(root + directory + " * " , storefile);
// For each subdirectory found, add in the base path.
for ( int j = 0 ; j < more.Length; j ++ )
more[j] = directory + more[j];
// Insert the subdirectorIEs into the List and
// update the counter and upper bound.
directoryList.InsertRange(i + 1 , more);
i += more.Length;
max += more.Length;
}
return ( string [])directoryList.ToArray();
}
/// <summary>
/// 下面代码从网上获得,用于递归指定路径下的所有文件
/// </summary>
/// <param name="pattern"></param>
/// <param name="storefile"></param>
/// <returns></returns>
public string [] GetAllfiles( string pattern, System.IO.IsolatedStorage.IsolatedStoragefile storefile)
{
// Get the root and file portions of the search string.
string fileString = System.IO.Path.Getfilename(pattern);
string [] files;
files = storefile.Getfilenames(pattern);
List < string > fileList = new List < string > (files);
// Loop through the subdirectorIEs, collect matches,
// and make separators consistent.
// foreach (string directory in GetAllDirectorIEs("*", storefile))
// foreach (string file in storefile.Getfilenames(directory + "/" + fileString))
// fileList.Add((directory + "/" + file));
return ( string [])fileList.ToArray();
}
/// <summary>
/// 当用户选择不同的路径时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private voID DirectoryList_SelectionChanged( object sender, SelectionChangedEventArgs e)
{
fileList.Items.Clear();
using (var store = System.IO.IsolatedStorage.IsolatedStoragefile.GetUserStoreForApplication())
{
foreach ( string file in GetAllfiles(DirectoryList.SelectedItem.ToString() + " / " , store))
{
fileList.Items.Add(file);
}
}
}
/// <summary>
/// 当用户选择相应的文件时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private voID fileList_SelectionChanged( object sender, SelectionChangedEventArgs e)
{
if (fileList.SelectedItem != null )
{
using (var store = System.IO.IsolatedStorage.IsolatedStoragefile.GetUserStoreForApplication())
{
string filePath = System.IO.Path.Combine(DirectoryList.SelectedItem.ToString(), fileList.SelectedItem.ToString());
if (store.fileExists(filePath))
{
IsolatedStoragefileStream subDraftfile =
store.Openfile(filePath, fileMode.Open);
// 加载指定文件中的内容
using (StreamReader reader = new StreamReader(subDraftfile))
{
string [] emailinfo = SplitString(reader.ReadToEnd(), " \r\n " );
foreach ( string emailitem in emailinfo)
{
if (emailitem.IndexOf( " RecipIEnt " ) >= 0 )
{
RecipIEnt.Text = emailitem.Split( ' : ' )[ 1 ]; continue ;
}
if (emailitem.IndexOf( " Subject " ) >= 0 )
{
Subject.Text = emailitem.Split( ' : ' )[ 1 ]; continue ;
}
if (emailitem.IndexOf( " Body " ) >= 0 )
{
Body.Text = emailitem.Split( ' : ' )[ 1 ]; continue ;
}
}
}
subDraftfile.Close();
}
else
{
HTMLPage.Window.Alert( " 文件: " + filePath + " 不存在!!! " );
}
}
}
}
}
其实就我个人而言,我是不太建议过于频繁的使用Isolate Store,当然主要是出于安全问题的考虑(
因为我本人对它的机制还不是很了解,所以不便妄加评论),但我本人还是比较偏向于下面这篇文章中的观
点: 临时数据存储可以考虑的方法――IsolatedStorage
好了,今天的内容就先到这里了:)
源代码下载链接,请点击这里:) 总结
以上是内存溢出为你收集整理的在silverlight中使用IsolateStore隔离存储(下)全部内容,希望文章能够帮你解决在silverlight中使用IsolateStore隔离存储(下)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)