答案:查找现有的文件和目录
您还可以使用独立存储文件来搜索现有的目录和文件。请记住,在存储区中,文件名和目录名是相对于虚文件系统的根目录指定的。此外,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[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(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 + "/" + fileSt
上一个:使用 .NET的IO(4) Paul_Ni(原作)
下一个:使用 .NET的IO(3) Paul_Ni(原作)