.net教程:使用 .net的io(5) paul_ni
查找现有的文件和目录
您还可以使用独立存储文件来搜索现有的目录和文件。请记住,在存储区中,文件名和目录名是相对于虚文件系统的根目录指定的。此外,windows 文件系统中的文件和目录名不区分大小写。
要搜索某个目录,请使用 isolatedstoragefile 的 getdirectorynames 实例方法。getdirectorynames 采用表示搜索模式的字符串。支持使用单字符 (?) 和多字符 (*) 通配符。这些通配符不能出现在名称的路径部分。也就是说,directory1/*ect* 是有效的搜索字符串,而 *ect*/directory2 不是有效的搜索字符串。
要搜索某个文件,请使用 isolatedstoragefile 的 getfilenames 实例方法。对应用于 getdirectorynames 的搜索字符串中通配符的相同限制也适用于 getfilenames。
getdirectorynames 和 getfilenames 都不是递归的,即 isolatedstoragefile 不提供用于列出存储区中所有目录或文件的方法。但是,下面的代码中部分是递归方法的示例。另外还要注意,getdirectorynames 和 getfilenames 只返回找到的项的目录名或文件名。例如,如果找到目录 rootdir/subdir/subsubdir 的匹配项,结果数组中将返回 subsubdir。
findingexistingfilesanddirectories 示例
下面的代码示例阐释如何在独立存储区创建文件和目录。首先,检索一个按用户、域和程序集隔离的存储区并放入 isostore 变量。createdirectory 方法用于设置几个不同的目录,isolatedstoragefilestream 方法在这些目录中创建一些文件。然后,代码依次通过 getalldirectories 方法的结果。该方法使用 getdirectorynames 来查找当前目录中的所有目录名。这些名称存储在数组中,然后 getalldirectories 调用其本身,传入它所找到的每个目录。结果是在数组中返回的所有目录名。然后,代码调用 getallfiles 方法。该方法调用 getalldirectories 以查找所有目录的名称,然后它检查每个目录以查找使用 getfilenames 方法的文件。结果返回到数组中用于显示。
[c#]
using system;
using system.io;
using system.io.isolatedstorage;
using system.collections;
public class findingexistingfilesanddirectories{
// retrieves an array of all directories in the store, and
// displays the results.
public static void main(){
// this part of the code sets up a few directories and files in the
// store.
isolatedstoragefile isostore = isolatedstoragefile.getstore(isolatedstoragescope.user | isolatedstoragescope.assembly, null, null);
isostore.createdirectory("topleveldirectory");
isostore.createdirectory("topleveldirectory/secondlevel");
isostore.createdirectory("anothertopleveldirectory/insidedirectory");
new isolatedstoragefilestream("intheroot.txt", filemode.create, isostore);
new isolatedstoragefilestream("anothertopleveldirectory/insidedirectory/hereiam.txt", filemode.create, isostore);
// end of setup.
console.writeline('\r');
console.writeline("here is a list of all directories in this isolated store:");
foreach(string directory in getalldirectories("*", isostore)){
console.writeline(directory);
}
console.writeline('\r');
// retrieve all the files in the directory by calling the getfiles
// method.
console.writeline("here is a list of all the files in this isolated store:");
foreach(string file in getallfiles("*", isostore)){
console.writeline(file);
}
}// end of main.
// method to retrieve all directories, recursively, within a store.
public static string[] getalldirectories(string pattern, isolatedstoragefile storefile){
// get the root of the search string.
string root = path.getdirectoryname(pattern);
if (root != "") root += "/";
// retrieve directories.
string[] directories;
directories = storefile.getdirectorynames(pattern);
arraylist directorylist = new arraylist(directories);
// retrieve subdirectories of matches.
for (int i = 0, max = directories.length; i < max; i++){
string directory = directorylist + "/";
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(type.gettype("system.string"));
}
public static string[] getallfiles(string pattern, isolatedstoragefile storefile){
// get the root and file portions of the search string.
string filestring = path.getfilename(pattern);
string[] files;
files = storefile.getfilenames(pattern);
arraylist filelist = new arraylist(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(type.gettype("system.string"));
}// end of getfiles.
}
读取和写入文件
使用 isolatedstoragefilestream 类,有多种方法可以打开存储区中的文件。一旦获得了 isolatedstoragefilestream 之后,可使用它来获取 streamreader 或 streamwriter。使用 streamreader 和 streamwriter,您可以像对任何其他文件一样读取和写入存储区中的文件。
readingandwritingtofiles 示例
下面的代码示例获得独立存储区,创建一个名为 teststore.txt 的文件并将“hello isolated storage”写入文件。然后,代码读取该文件并将结果输出到控制台。
[c#]
using system;
using system.io;
using system.io.isolatedstorage;
public class readingandwritingtofiles{
public static int 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);
// this code checks to see if the file already exists.
string[] filenames = isostore.getfilenames("teststore.txt");
foreach (string file in filenames){
if(file == "teststore.txt"){
console.writeline("the file already exists!");
console.writeline("type \"storeadm /remove\" at the command line to delete all isolated storage for this user.");
// exit the program.
return 0;
}
}
writetofile(isostore);
console.writeline("the file \"teststore.txt\" contains:");
// call the readfromfile and write the returned string to the
//console.
console.writeline(readfromfile(isostore));
// exit the program.
return 0;
}// end of main.
// this method writes "hello isolated storage" to the file.
private static void writetofile(isolatedstoragefile isostore){
// declare a new streamwriter.
streamwriter writer = null;
// assign the writer to the store and the file teststore.
writer = new streamwriter(new isolatedstoragefilestream("teststore.txt", filemode.createnew,isostore));
// have the writer write "hello isolated storage" to the store.
writer.writeline("hello isolated storage");
writer.close();
console.writeline("you have written to the file.");
}// end of writetofile.
// this method reads the first line in the "teststore.txt" file.
public static string readfromfile(isolatedstoragefile isostore){
// this code opens the teststore.txt file and reads the string.
streamreader reader = new streamreader(new isolatedstoragefilestream("teststore.txt", filemode.open,isostore));
// read a line from the file and add it to sb.
string sb = reader.readline();
// close the reader.
reader.close();
// return the string.
return sb.tostring();
}// end of readfromfile.
}
删除文件和目录
您可以删除独立存储文件中的目录和文件。请记住,在存储区中,文件名和目录名是与操作系统相关的(在 microsoft windows 系统中通常不区分大小写),并且是根据虚文件系统的根目录具体而定的。
isolatedstorefile 类提供了两种删除目录和文件的实例方法:deletedirectory 和 deletefile。如果尝试删除并不存在的文件和目录,则会引发 isolatedstoragefileexception。如果名称中包含有通配符,则 deletedirectory 会引发 isolatedstoragefileexception,而 deletefile 将引发 argumentexception。
如果目录中包含任何文件或子目录,deletedirectory 将会失败。在 deletingfilesanddirectories 示例的一部分中定义了一个方法,该方法删除目录中的所有内容,然后删除目录本身。同样,您可以自己定义一个接受通配符的 deletefiles 方法,该方法可以这样来实现:使用 getfilenames 方法获取所有匹配文件的列表,然后依次删除每个文件。
deletingfilesanddirectories 示例
下面的代码示例先创建若干个目录和文件,然后将它们删除。
[c#]
using system;
using system.io.isolatedstorage;
using system.io;
public class deletingfilesdirectories{
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);
console.writeline("creating directories:");
// this code creates several different directories.
isostore.createdirectory("topleveldirectory");
console.writeline("topleveldirectory");
isostore.createdirectory("topleveldirectory/secondlevel");
console.writeline("topleveldirectory/secondlevel");
// this code creates two new directories, one inside the other.
isostore.createdirectory("anothertopleveldirectory/insidedirectory");
console.writeline("anothertopleveldirectory/insidedirectory");
console.writeline();
// this code creates a few files and places them in the directories.
console.writeline("creating files:");
// this file is placed in the root.
isolatedstoragefilestream isostream1 = new isolatedstoragefilestream("intheroot.txt", filemode.create, isostore);
console.writeline("intheroot.txt");
isostream1.close();
// this file is placed in the insidedirectory.
isolatedstoragefilestream isostream2 = new isolatedstoragefilestream("anothertopleveldirectory/insidedirectory/hereiam.txt", filemode.create, isostore);
console.writeline("anothertopleveldirectory/insidedirectory/hereiam.txt");
console.writeline();
isostream2.close();
console.writeline("deleting file:");
// this code deletes the hereiam.txt file.
isostore.deletefile("anothertopleveldirectory/insidedirectory/hereiam.txt");
console.writeline("anothertopleveldirectory/insidedirectory/hereiam.txt");
console.writeline();
console.writeline("deleting directory:");
// this code deletes the insidedirectory.
isostore.deletedirectory("anothertopleveldirectory/insidedirectory/");
console.writeline("anothertopleveldirectory/insidedirectory/");
console.writeline();
}// end of main.
}
总结
上面是vs.net中.net中io的基本概念、示例代码以及访问文件系统的基础方法和流程,大家可以多多实践。有任何建议请mail我 paulni@citiz.net(paulni@citiz.net)。