IsolatedStorageFileisolatedStorageSettingWord格式.docx

上传人:b****6 文档编号:20695487 上传时间:2023-01-25 格式:DOCX 页数:13 大小:18.86KB
下载 相关 举报
IsolatedStorageFileisolatedStorageSettingWord格式.docx_第1页
第1页 / 共13页
IsolatedStorageFileisolatedStorageSettingWord格式.docx_第2页
第2页 / 共13页
IsolatedStorageFileisolatedStorageSettingWord格式.docx_第3页
第3页 / 共13页
IsolatedStorageFileisolatedStorageSettingWord格式.docx_第4页
第4页 / 共13页
IsolatedStorageFileisolatedStorageSettingWord格式.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

IsolatedStorageFileisolatedStorageSettingWord格式.docx

《IsolatedStorageFileisolatedStorageSettingWord格式.docx》由会员分享,可在线阅读,更多相关《IsolatedStorageFileisolatedStorageSettingWord格式.docx(13页珍藏版)》请在冰豆网上搜索。

IsolatedStorageFileisolatedStorageSettingWord格式.docx

//ApplicationSettings特定于某个用户和某个应用程序。

应用程序范围由应用程序的完整路径决定。

privatevoidInitializeSettings()

{

if(settings.Contains("

emailFlag"

))

EmailFlag.IsChecked=(bool)settings["

];

}

elsesettings.Add("

false);

privatevoidEmailFlag_Unchecked(objectsender,RoutedEventArgse)

settings["

]=false;

privatevoidEmailFlag_Checked(objectsender,RoutedEventArgse)

]=true;

}

(2)IsolatedStorageFile

IsolatedStorageFile表示包含文件和目录的独立存储区。

使用IsolatedStorageFile是一种让你可以在用户的设备中存储真实文件的机制。

该类使独立存储的虚拟文件系统抽象化。

IsolatedStorageFile对象对应于特定的独立存储范围,在该范围中存在由IsolatedStorageFileStream对象表示的文件。

应用程序可以使用独立存储将数据保存在文件系统中这些数据自己的独立部分,而不必在文件系统中指定特定的路径。

虚拟文件系统的根位于物理文件系统上经过模糊处理的每用户文件夹中。

由主机提供的每个唯一标识符都映射为不同的根,该根为每个应用程序提供它自己的虚拟文件系统。

应用程序不能从它自己的文件系统导航到另一个文件系统中。

因为独立存储区在特定程序集的范围内,所以其他大多数托管代码都不能访问您的代码的数据(高度受信任的托管代码和管理工具可以从其他程序集访问存储区)。

非托管代码可以访问任何独立存储区。

例子:

在一个子目录中创建了一个文本文件,并读取文件中的内容。

我们还可以创建和删除目录,子目录及文件。

创建一个新的IsolatedStorageFile对象,并使用一个IsolatedStorageFileStream对象将它写入到驱动器中。

usingSystem.IO.IsolatedStorage;

usingSystem.IO;

privatevoidSaveButton_Click(objectsender,RoutedEventArgse)

//为程序获取一个虚拟的本地存储

IsolatedStorageFilefileStorage=IsolatedStorageFile.GetUserStoreForApplication();

//创建一个新的文件夹

fileStorage.CreateDirectory("

textFiles"

);

//创建一个txt文件的流

StreamWriterfileWriter=newStreamWriter(newIsolatedStorageFileStream("

textFiles\\newText.txt"

FileMode.OpenOrCreate,fileStorage));

//向文件中写出内容

fileWriter.WriteLine(writeText.Text);

//关闭StreamWriter.

fileWriter.Close();

privatevoidGetButton_Click(objectsender,RoutedEventArgse)

//创建一个新的StreamReader

StreamReaderfileReader=null;

try

{

//读取文件

fileReader=newStreamReader(newIsolatedStorageFileStream("

FileMode.Open,fileStorage));

//读取内容

stringtextFile=fileReader.ReadLine();

viewText.Text=textFile;

fileReader.Close();

catch

viewText.Text="

Needtocreatedirectoryandthefilefirst."

;

usingSystem;

usingSystem.Windows;

usingSystem.Windows.Controls;

usingSystem.Windows.Input;

usingSystem.Collections;

usingSystem.Text;

classExample

publicstaticvoidDemo(TextBlockoutputBlock)

//Obtainanisolatedstoreforanapplication.

using(varstore=IsolatedStorageFile.GetUserStoreForApplication())

//UseaStringBuildertoconstructoutput.

StringBuildersb=newStringBuilder();

//Createthreedirectoriesintheroot.

store.CreateDirectory("

MyApp1"

MyApp2"

MyApp3"

//CreatethreesubdirectoriesunderMyApp1.

stringsubdirectory1=Path.Combine("

"

SubDir1"

stringsubdirectory2=Path.Combine("

SubDir2"

stringsubdirectory3=Path.Combine("

SubDir3"

store.CreateDirectory(subdirectory1);

store.CreateDirectory(subdirectory2);

store.CreateDirectory(subdirectory3);

//Createafileintheroot.

IsolatedStorageFileStreamrootFile=store.CreateFile("

InTheRoot.txt"

rootFile.Close();

//Createafileinasubdirectory.

IsolatedStorageFileStreamsubDirFile=

store.CreateFile(Path.Combine(subdirectory1,"

MyApp1A.txt"

));

subDirFile.Close();

//Gatherfileinformation

string[]directoriesInTheRoot=store.GetDirectoryNames();

string[]filesInTheRoot=store.GetFileNames();

stringsearchpath=Path.Combine(subdirectory1,"

*.*"

string[]filesInSubDirs=store.GetFileNames(searchpath);

//FindsubdirectorieswithintheMyApp1

//directoryusingthemulticharacter'

*'

wildcard.

string[]subDirectories=

store.GetDirectoryNames(Path.Combine("

*"

//Listfileinformation

//Listthedirectoriesintheroot.

sb.AppendLine("

Directoriesinroot:

"

foreach(stringdirindirectoriesInTheRoot)

-"

+dir);

sb.AppendLine();

//ListthesubdirectoriesunderMyApp1.

DirectoriesunderMyApp1:

foreach(stringsDirinsubDirectories)

+sDir);

//Listfilesintheroot.

Filesintheroot:

foreach(stringfileNameinfilesInTheRoot)

+fileName);

//ListfilesinMyApp1\SubDir1.

sb.AppendLine(@"

FilesinMyApp1\SubDir1:

foreach(stringfileNameinfilesInSubDirs)

//Writetoanexistingfile:

MyApp1\SubDir1\MyApp1A.txt

//Determineifthefileexistsbeforewritingtoit.

stringfilePath=Path.Combine(subdirectory1,"

if(store.FileExists(filePath))

using(StreamWritersw=

newStreamWriter(store.OpenFile(filePath,

FileMode.Open,FileAccess.Write)))

sw.WriteLine("

Todolist:

1.Buysupplies."

catch(IsolatedStorageExceptionex)

sb.AppendLine(ex.Message);

//Readthecontentsofthefile:

using(StreamReaderreader=

newStreamReader(store.OpenFile(filePath,

FileMode.Open,FileAccess.Read)))

stringcontents=reader.ReadToEnd();

sb.AppendLine(filePath+"

contents:

sb.AppendLine(contents);

//Deleteafile.

store.DeleteFile(filePath);

//Deleteaspecificdirectory.

stringdirDelete=Path.Combine("

if(store.DirectoryExists(dirDelete))

store.DeleteDirectory(dirDelete);

//removethestore

store.Remove();

Storeremoved."

outputBlock.Text=sb.ToString();

catch(IsolatedStorageException)

//TODO:

Handlethatstorewasunabletobeaccessed.

//Quotastatusandincreasequotaexamples

classStoreQuota

//AssumesaneventhandlerfortheMouseLeftbuttonUp

//eventisdefinedforacontrolnamed'

IncreaseQuota'

//Inthecontrol'

sXAML:

MouseLeftButtonUp="

IncreaseQuota_OnLeftMouseButtonUp"

//UserfirstselectsUItoincreasethequota.

publicvoidIncreaseQuota_OnClick(objectsender,MouseEventArgse)

//Request5MBmorespaceinbytes.

Int64spaceToAdd=5242880;

Int64curAvail=store.AvailableFreeSpace;

//Ifavailablespaceislessthan

//whatisrequested,trytoincrease.

if(curAvail<

spaceToAdd)

//Requestmorequotaspace.

if(!

store.IncreaseQuotaTo(store.Quota+spaceToAdd))

//TheuserclickedNOtothe

//host'

sprompttoapprovethequotaincrease.

else

//TheuserclickedYEStothe

Handlethatstorecouldnotbeaccessed.

publicstaticvoidShowIsoStoreStatus(TextBlockinputBlock)

stringspaceUsed=(store.Quota-store.AvailableFreeSpace).ToString();

stringspaceAvailable=store.AvailableFreeSpace.ToString();

stringcurQuota=store.Quota.ToString();

inputBlock.Text=

String.Format("

Quota:

{0}bytes,Used:

{1}bytes,Available:

{2}bytes"

curQuota,spaceUsed,spaceAvailable);

inputBlock.Text="

Unabletoaccessstore."

//Thisexample'

sExample.Demomethod

//producesthefollowingoutput:

//-----

//Directoriesinroot:

//-MyApp1

//-MyApp2

//-MyApp3

//DirectoriesunderMyApp1:

//-SubDir1

//-SubDir2

//-SubDir3

//Filesintheroot:

//-InTheRoot.txt

//FilesinMyApp1\SubDir1:

//-MyApp1A.txt

//MyApp1\SubDir1\MyApp1A.txtcontents:

//Todolist:

//1.Buysupplies.

//Storeremoved.

usingSystem.Linq;

namespaceIsolatedStorageSample

publicpartialclassPage:

UserControl

privateIsolatedStorageSetti

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 建筑土木

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1