大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> 使用 .NET的IO(4) Paul_Ni(原作)

使用 .NET的IO(4) Paul_Ni(原作)

时间: 2021-07-31 作者:daque

赢得保存区保存区公然数据舱中的虚文献体例。isolatedstoragefile 供给了很多与保存区举行交互的本领。要创造和检索保存区,isolatedstoragefile 供给了三种静态本领。挪用 getuserstoreforassembly 或 getuserstorefordomain 辨别归来按用户和步调集分隔及按用户、域和步调集分隔的保存。这两种本领检索属于代码块(是从该代码块中挪用这两种本领的)的保存区。静态本领 getstore 归来独力保存区,该保存区是经过传入范畴参数拉拢指定的。底下的参数归来一个按用户、步调集和域分隔的保存区。[c#]getstore(isolatedstoragescope.user | isolatedstoragescope.assembly | isolatedstoragescope.domain, null, null);getstore 本领不妨用来指定保存区该当和遨游用户摆设文献一道遨游。默许情景下,从各别的步调会合赢得的独力保存区是各别的。您不妨考察各别步调集或域的保存区,本领是传入各别的步调集或域证明动作 getstore 本领的结果两个参数。这须要考察按运用步调域标识分隔的独力保存的权力。相关更多消息,请参见 getstore 本领。相关步调集的更多消息,请参见步调集。三种本领中的每种本领都归来 isolatedstoragefile 东西。一旦具备了独力保存文献东西之后,您便不妨运用独力保存本领来读取、写入、创造和简略文献及文献目次了。没有提防代码向没有充满考察权力来本人获得保存区的代码传播 isolatedstoragefile 的体制。惟有当赢得对 isolatedstorage 东西的援用时(常常是在 getuserstoreforassembly、getuserstorefordomain 或 getstore 本领中),才查看域和步调集标识及独力保存权力。所以,运用那些援用的代码该当养护对 isolatedstoragefile 东西的援用。obtainingastore 示例底下的代码示例是一个特殊大略的由类赢得按用户和步调集分隔的保存区的示例。经过向 getstore 本领传播的参数增添 isolatedstoragescope.domain,此代码可被变动用来检索按用户、域和步调集分隔的保存区。 运转代码之后,您不妨经过在吩咐行键入 storeadm /list 来确认已创造了保存区。这将运转独力保存处置东西 (storeadm.exe) 并列出用户暂时一切的独力保存区。[c#]using system;using system.io.isolatedstorage;public class obtainingastore{ public static void main(){// get a new isolated store for this assembly and put it into an// isolated store object.isolatedstoragefile isostore =isolatedstoragefile.getstore(isolatedstoragescope.user | isolatedstoragescope.assembly, null, null); }}列举保存区您不妨运用 isolatedstoragefile 静态本领 getenumerator 列举暂时用户的一切独力保存区。getenumerator 取 isolatedstoragescope 值并归来 isolatedstoragefile 列举数。user 是独一受扶助的 isolatedstoragescope 值。要列举保存区,您必需具备指定 isolatedstoragecontainment 值 administerisolatedstoragebyuser的 isolatedstoragefilepermission。当运用 isolatedstoragescope 值 user 举行挪用时,getenumerator 归来为暂时用户设置的 isolatedstoragefiles 数组。enumeratingstores 示例底下的代码示例赢得按用户和步调集分隔的保存区并创造几个文献。挪用 getenumerator 本领并将截止放入 ienumerator。而后代码顺序经过 ienumerator,增添文献的巨细,并将截止汇报给遏制台。本质列举爆发在独占 enumeratethestore 本领中,为了领会起见,将该本领与代码的其余局部划分,放在文献的底部。[c#]using system;using system.io;using system.io.isolatedstorage;using system.collections;public class enumeratingstores{ public static int main(){// get an isolated store for this assembly and put it into an// isolatedstoragefile object.isolatedstoragefile isostore =isolatedstoragefile.getstore(isolatedstoragescope.user | isolatedstoragescope.assembly, null, null);// this code creates a few files so that they can be enumerated.isolatedstoragefilestream streama = new isolatedstoragefilestream("testfilea.txt", filemode.create, isostore);isolatedstoragefilestream streamb = new isolatedstoragefilestream("testfileb.txt", filemode.create, isostore);isolatedstoragefilestream streamc = new isolatedstoragefilestream("testfilec.txt", filemode.create, isostore);isolatedstoragefilestream streamd = new isolatedstoragefilestream("testfiled.txt", filemode.create, isostore);streama.close();streamb.close();streamc.close();streamd.close();// there might be a small delay between when the above code// executes and when the files are created in the store. // closing and opening the store in this example ensures that// the common language runtime has finished creating the files.isostore .close();isostore =isolatedstoragefile.getstore(isolatedstoragescope.user | isolatedstoragescope.assembly, null, null); // this line of code calls a method at the bottom of the program// that puts all the files in isostore into an ienumerator.ienumerator allfiles = enumeratethestore (isostore);long totalsize = 0;// this code counts up the sizes of all the stores.while(allfiles .movenext()){ isolatedstoragefile store = (isolatedstoragefile)allfiles.current; totalsize += (long)store.currentsize;}console.writeline("the total size = "+totalsize);return 0; } // this method returns an ienucontaining all the files for a user. private static ienumerator enumeratethestore(isolatedstoragefile isostore){ienumerator e = isolatedstoragefile.getenumerator(isolatedstoragescope.user);return e; }}简略保存区isolatedstoragefile 供给了两种简略独力保存文献的本领: 范例本领 remove 不取任何参数,简略挪用它的保存区。该操纵不须要任何权力。不妨考察保存区的任何代码都不妨简略该保存区中的任何数据或一切数据。 静态本领 remove 沿用 isolatedstoragescope 值 user,并简略运转该代码的用户的一切保存区。该操纵须要 isolatedstoragecontainment 值 administerisolatedstoragebyuser 的 isolatedstoragefilepermission 权力。 deletingstores 示例底下的代码示例演练了静态和范例 remove 本领的运用。类赢得两个保存区,一个按用户和步调集分隔;另一个按用户、域和步调集分隔。经过挪用 isolatedstoragefile isostore1 的 remove 本领简略用户、域和步调集保存区。而后,经过挪用静态本领 isolatedstoragefile.remove 简略该用户一切结余的保存区。[c#]using system;using system.io.isolatedstorage;public class deletingstores{ public static void main(){// get a new isolated store for this user, domain, and assembly.// put the store into an isolatedstoragefile object.isolatedstoragefile isostore1 =isolatedstoragefile.getstore(isolatedstoragescope.user | isolatedstoragescope.domain | isolatedstoragescope.assembly, null, null);console.writeline("a store isolated by user, assembly, and domain has been obtained.");// get a new isolated store for user and assembly.// put that store into a different isolatedstoragefile object.isolatedstoragefile isostore2 = isolatedstoragefile.getstore(isolatedstoragescope.user | isolatedstoragescope.assembly, null, null);console.writeline("a store isolated by user and assembly has been obtained.");// the remove method deletes a specific store, in this case the// isostore1 file.isostore1.remove();console.writeline("the user, domain, and assembly isolated store has been deleted.");// this static method deletes all the isolated stores for this user.isolatedstoragefile.remove(isolatedstoragescope.user);console.writeline("all isolated stores for this user have been deleted."); }// end of main.}预示空间不及的情景运用独力保存的代码受分配的定额的控制,该分配的定额指定独力保存文献和目次地方的数据舱的最大巨细。该值由安定战略决定,处置员不妨对其举行摆设。即使试图写入数据时胜过了所承诺的最大巨细,将激励 isolatedstorageexception,并使操纵波折。这无助于于提防歹意的抵克服务报复,遭到这种报复后会由于数据保存被填满而引导运用步调中断乞求。为了扶助您决定给定的写入试验能否会由于此原所以波折,独力保存供给了两个只读属性:isolatedstorage.currentsize 和 isolatedstorage.maximumsize。这两个属性可用来决定写入保存区能否将引导胜过保存区所承诺的最大巨细。当您运用那些属性时,请记取独力保存大概被同声考察;所以,即使您计划的保存量有结余,则该保存空间大概在您试图写入保存区时已被运用。然而,这不会妨害您运用保存区的最大巨细来决定能否将到达可用保存的下限。另一个要害的商量是最大巨细属性在于于来自平常处事的步调集的证明。所以,只该当对运用 getuserstoreforassembly()、getuserstorefordomain() 或 getstore() 创造的 isolatedstoragefile 东西挪用此本领。以其余任何办法(比方从 getenumerator() 中归来)创造的 isolatedstoragefile 东西将没辙归来精确的最大巨细。 anticipatingoutofspaceconditions 示例底下的代码示例赢得一个独力保存区,创造几个文献并襟怀保存区中结余的空间。以字节数汇报结余的空间。[c#]using system;using system.io;using system.io.isolatedstorage;public class checkingspace{ public static void main(){// get an isolated store for this assembly and put it into an// isolatedstorefile object.isolatedstoragefile isostore =isolatedstoragefile.getstore(isolatedstoragescope.user | isolatedstoragescope.assembly, null, null);// create a few placeholder files in the isolated store.new isolatedstoragefilestream("intheroot.txt", filemode.create, isostore);new isolatedstoragefilestream("another.txt", filemode.create, isostore);new isolatedstoragefilestream("athird.txt", filemode.create, isostore);new isolatedstoragefilestream("afourth.txt", filemode.create, isostore);new isolatedstoragefilestream("afifth.txt", filemode.create, isostore);// use the currentsize and maximumsize methods to find remaining // space.// cast that number into a long type and put it into a variable.long spaceleft =(long)(isostore.maximumsize - isostore.currentsize);console.writeline(spaceleft+ " bytes of space remain in this isolated store."); }// end of main.}创造文献和目次赢得保存区之后,您不妨创造用来保存数据的目次和文献。在保存区中,文献名和目次名是对立于虚文献体例的根目次指定的。要创造目次,请运用 isolatedstoragefile 的 createdirectory 范例本领。即使您指定一个未创造目次的子目次,则会同声创造两个目次。即使您指定一个已生存的目次,将不会天生任何特殊。然而,即使您指定一个包括失效字符的目次称呼,则会天生 isolatedstorageexception。要创造并翻开文献,请运用 isolatedstoragefilestream 结构因变量之一,传入文献名、filemode 值 openorcreate 和要在个中创造文献的保存区。而后,您不妨在文献流中对数据实行想要实行的操纵,比方读取、探求和写入。isolatedstoragefilestream 结构因变量还可用来为其余手段翻开文献。经过运用任何不取 isolatedstoragefile 参数的 isolatedstoragefilestream 结构因变量,您还不妨在不开始赢得保存区的情景下创造或翻开文献。当运用这种情势的结构因变量时,文献是在该文献的域保存区中创造的。在 windows 文献体例中,为了对称呼举行比拟,独力保存文献和目次名都不辨别巨细写。如许,即使您创造了一个名为 thisfile.txt 的文献,而后又创造了名为 thisfile.txt 的另一个文献,本质上只创造了一个文献。表露时,文献名维持其原有的巨细写。 creatingfilesanddirectories 示例底下的代码示例阐释怎样在独力保存区创造文献和目次。开始,检索一个按用户、域和步调集分隔的保存区并放入 isostore 变量。createdirectory 本领用来树立少量各别的目次,而 isolatedstoragefilestream 本领在那些目次中创造少许文献。[c#]using system;using system.io;using system.io.isolatedstorage;public class creatingfilesdirectories{ public static void main(){// get a new isolated store for this user, domain, and assembly.// put the store into an isolatedstoragefile object.isolatedstoragefile isostore =isolatedstoragefile.getstore(isolatedstoragescope.user | isolatedstoragescope.domain | isolatedstoragescope.assembly, null, null);// this code creates a few different directories.isostore.createdirectory("topleveldirectory");isostore.createdirectory("topleveldirectory/secondlevel");// this code creates two new directories, one inside the other.isostore.createdirectory("anothertopleveldirectory/insidedirectory");// this file is placed in the root.isolatedstoragefilestream isostream1 = new isolatedstoragefilestream("intheroot.txt", filemode.create, isostore);console.writeline("created a new file in the root.");isostream1.close();// this file is placed in the insidedirectory.isolatedstoragefilestream isostream2 = new isolatedstoragefilestream("anothertopleveldirectory/insidedirectory/hereiam.txt", filemode.create, isostore);isostream2.close();console.writeline("created a new file in the insidedirectory."); }// end of main.}

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.