격리된 저장소 파일을 사용하여 기존의 디렉터리와 파일을 검색할 수도 있습니다. 저장소 내에서 파일 및 디렉터리 이름은 가상 파일 시스템의 루트와 관련하여 지정되며 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 메서드를 사용하여 각 디렉터리를 검사하여 파일을 확인합니다. 결과는 배열로 반환됩니다.
Imports System
Imports System.IO
Imports System.IO.IsolatedStorage
Imports System.Collections
Imports System.Collections.Generic
Public class FindingExistingFilesAndDirectories
' These arrayLists hold the directory and file names as they are found.
Private Shared directoryList As New List(Of String)
Private Shared fileList As New List(Of String)
' Retrieves an array of all directories in the store, and
' displays the results.
Public Shared Sub Main()
' This part of the code sets up a few directories and files in the store.
Dim isoStore As IsolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or _
IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, Nothing, Nothing)
isoStore.CreateDirectory("TopLevelDirectory")
isoStore.CreateDirectory("TopLevelDirectory/SecondLevel")
isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory")
Dim aStream As New IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore)
Dim bStream As New IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", _
FileMode.Create, isoStore)
' End of setup.
Console.WriteLine()
Console.WriteLine("Here is a list of all directories in this isolated store:")
GetAllDirectories("*", isoStore)
For Each directory As String In directoryList
Console.WriteLine(directory)
Next
Console.WriteLine()
Console.WriteLine("Retrieve all the files in the directory by calling the GetFiles method.")
GetAllFiles(isoStore)
For Each file As String In fileList
Console.WriteLine(file)
Next
End Sub
Public Shared Sub GetAllDirectories(ByVal pattern As String, ByVal storeFile As IsolatedStorageFile)
' Retrieve directories.
Dim directories As String() = storeFile.GetDirectoryNames(pattern)
For Each directory As String In directories
' Add the directory to the final list.
directoryList.Add((pattern.TrimEnd(CChar("*"))) + directory + "/")
' Call the method again using directory.
GetAllDirectories((pattern.TrimEnd(CChar("*")) + directory + "/*"), storeFile)
Next
End Sub
Public Shared Sub GetAllFiles(ByVal storefile As IsolatedStorageFile)
' This adds the root to the directory list.
directoryList.Add("*")
For Each directory As String In directoryList
Dim files As String() = storefile.GetFileNames(directory + "*")
For Each dirfile As String In files
fileList.Add(dirfile)
Next
Next
End Sub
End Class
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections;
using System.Collections.Generic;
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 List<String> GetAllDirectories(string pattern, IsolatedStorageFile storeFile)
{
// Get the root of the search string.
string root = Path.GetDirectoryName(pattern);
if (root != "")
{
root += "/";
}
// Retrieve directories.
List<String> directoryList = new List<String>(storeFile.GetDirectoryNames(pattern));
// Retrieve subdirectories of matches.
for (int i = 0, max = directoryList.Count; i < max; i++)
{
string directory = directoryList[i] + "/";
List<String> more = GetAllDirectories(root + directory + "*", storeFile);
// For each subdirectory found, add in the base path.
for (int j = 0; j < more.Count; 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.Count;
max += more.Count;
}
return directoryList;
}
public static List<String> GetAllFiles(string pattern, IsolatedStorageFile storeFile)
{
// Get the root and file portions of the search string.
string fileString = Path.GetFileName(pattern);
List<String> fileList = new List<String>(storeFile.GetFileNames(pattern));
// 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 fileList;
} // End of GetFiles.
}
using namespace System;
using namespace System::IO;
using namespace System::IO::IsolatedStorage;
using namespace System::Collections;
using namespace System::Collections::Generic;
public class FindingExistingFilesAndDirectories
{
public:
// Retrieves an array of all directories in the store, and
// displays the results.
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, (Type ^)nullptr, (Type ^)nullptr);
isoStore->CreateDirectory("TopLevelDirectory");
isoStore->CreateDirectory("TopLevelDirectory/SecondLevel");
isoStore->CreateDirectory("AnotherTopLevelDirectory/InsideDirectory");
gcnew IsolatedStorageFileStream("InTheRoot.txt", FileMode::Create, isoStore);
gcnew 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:");
for each (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:");
for each (String^ file in GetAllFiles("*", isoStore))
{
Console::WriteLine(file);
}
} // End of Main.
// Method to retrieve all directories, recursively, within a store.
static List<String^>^ GetAllDirectories(String^ pattern, IsolatedStorageFile^ storeFile)
{
// Get the root of the search string.
String^ root = Path::GetDirectoryName(pattern);
if (root != "")
{
root += "/";
}
// Retrieve directories.
array<String^>^ directories = storeFile->GetDirectoryNames(pattern);
List<String^>^ directoryList = gcnew List<String^>(directories);
// Retrieve subdirectories of matches.
for (int i = 0, max = directories->Length; i < max; i++)
{
String^ directory = directoryList[i] + "/";
List<String^>^ more = GetAllDirectories (root + directory + "*", storeFile);
// For each subdirectory found, add in the base path.
for (int j = 0; j < more->Count; 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->Count;
max += more->Count;
}
return directoryList;
}
static List<String^>^ GetAllFiles(String^ pattern, IsolatedStorageFile^ storeFile)
{
// Get the root and file portions of the search string.
String^ fileString = Path::GetFileName(pattern);
array<String^>^ files = storeFile->GetFileNames(pattern);
List<String^>^ fileList = gcnew List<String^>(files);
// Loop through the subdirectories, collect matches,
// and make separators consistent.
for each (String^ directory in GetAllDirectories( "*", storeFile))
{
for each (String^ file in storeFile->GetFileNames(directory + "/" + fileString))
{
fileList->Add((directory + "/" + file));
}
}
return fileList;
} // End of GetFiles.
};
int main()
{
FindingExistingFilesAndDirectories::Main();
}