Freigeben über


Gewusst wie: Suchen von vorhandenen Dateien und Verzeichnissen im isolierten Speicher

Sie können auch nach vorhandenen Verzeichnissen und Dateien suchen und dazu eine Datei in isolierter Speicherung verwenden. Beachten Sie, dass Datei- und Verzeichnisnamen im Speicher in Bezug auf den Stamm des virtuellen Dateisystems angegeben werden. Außerdem wird in den Windows-Dateisystemen in Datei- und Verzeichnisnamen keine Groß- und Kleinschreibung berücksichtigt.

Um nach einem Verzeichnis zu suchen, verwenden Sie die GetDirectoryNames-Instanzmethode von IsolatedStorageFile. GetDirectoryNames lässt eine Zeichenfolge zu, die ein Suchmuster darstellt. Es werden Platzhalter sowohl für Einzelzeichen (?) als auch für mehrere Zeichen (*) unterstützt. Die Platzhalterzeichen dürfen nicht in der Pfadangabe des Namens vorkommen. Ein Beispiel: directory1/*ect* ist eine gültige Suchzeichenfolge, *ect*/directory2 dagegen nicht.

Verwenden Sie zum Suchen nach einer Datei die GetFileNames-Instanzmethode von IsolatedStorageFile. Für GetFileNames gelten dieselben Einschränkungen bei Platzhalterzeichen wie für GetDirectoryNames.

Weder GetDirectoryNames noch GetFileNames sind rekursiv: Methoden zum Auflisten aller Verzeichnisse und Dateien in Ihrem Speicher werden von IsolatedStorageFile nicht unterstützt. Allerdings sind Beispiele für rekursive Methoden im unten angegebenen Code enthalten. Beachten Sie auch, dass GetDirectoryNames und GetFileNames nur den Verzeichnis- bzw. Dateinamen gefundener Elemente zurückgeben. Ein Beispiel: Wenn eine Übereinstimmung im Verzeichnis RootDir/SubDir/SubSubDir gefunden wird, wird SubSubDir im Array zurückgegeben.

Beispiel "FindingExistingFilesAndDirectories"

Im folgenden Codebeispiel wird das Erstellen von Dateien und Verzeichnissen in einem isolierten Speicher veranschaulicht. Zunächst wird ein nach Benutzer, Domäne und Assembly isolierter Speicher abgerufen und in der isoStore-Variable abgelegt. Die CreateDirectory-Methode wird zum Einrichten einiger verschiedener Verzeichnisse eingesetzt. Durch die IsolatedStorageFileStream-Methode werden einige Dateien in diesen Verzeichnissen erstellt. Der Code durchläuft dann die Ergebnisse der GetAllDirectories-Methode. Diese Methode verwendet GetDirectoryNames zur Suche aller Verzeichnisnamen im aktuellen Verzeichnis. Diese Namen werden in einem Array gespeichert. GetAllDirectories ruft sich danach selbst auf und übergibt jedes gefundene Verzeichnis. Das Ergebnis enthält alle Verzeichnisnamen, die in einem Array zurückgegeben werden. Anschließend wird durch den Code die GetAllFiles-Methode aufgerufen. Diese Methode ruft GetAllDirectories zur Suche nach den Namen aller Verzeichnisse auf und durchsucht anschließend mithilfe der GetFileNames-Methode jedes Verzeichnis nach Dateien. Das Ergebnis wird zur Darstellung in einem Array zurückgegeben.

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();
}

Siehe auch

Referenz

IsolatedStorageFile

Konzepte

Isolierte Speicherung