次の方法で共有


方法: 分離ストレージ内の既存のファイルとディレクトリを検索する

分離ストレージ内のディレクトリを検索するには、 IsolatedStorageFile.GetDirectoryNames メソッドを使用します。 このメソッドは、検索パターンを表す文字列を受け取ります。 検索パターンでは、単一文字 (?) と複数文字 (*) の両方のワイルドカード文字を使用できますが、名前の最後の部分にワイルドカード文字を含める必要があります。 たとえば、 directory1/*ect* は有効な検索文字列ですが、 *ect*/directory2 ではありません。

ファイルを検索するには、 IsolatedStorageFile.GetFileNames メソッドを使用します。 GetDirectoryNamesに適用される検索文字列のワイルドカード文字の制限は、GetFileNamesにも適用されます。

これらのメソッドはどちらも再帰的ではありません。 IsolatedStorageFile クラスは、ストア内のすべてのディレクトリまたはファイルを一覧表示するためのメソッドを提供しません。 ただし、再帰メソッドを次のコード例に示します。

次のコード例は、分離ストアにファイルとディレクトリを作成する方法を示しています。 まず、ユーザー、ドメイン、およびアセンブリ用に分離されたストアが取得され、 isoStore 変数に配置されます。 CreateDirectory メソッドは、いくつかの異なるディレクトリを設定するために使用され、IsolatedStorageFileStream(String, FileMode, IsolatedStorageFile) コンストラクターはこれらのディレクトリにいくつかのファイルを作成します。 その後、コードは、 GetAllDirectories メソッドの結果をループします。 このメソッドでは、 GetDirectoryNames を使用して、現在のディレクトリ内のすべてのディレクトリ名を検索します。 これらの名前は配列に格納され、 GetAllDirectories はそれ自体を呼び出し、見つかった各ディレクトリを渡します。 その結果、すべてのディレクトリ名が配列に返されます。 次に、 GetAllFiles メソッドを呼び出します。 このメソッドは GetAllDirectories を呼び出してすべてのディレクトリの名前を確認し、 GetFileNames メソッドを使用して各ディレクトリでファイルをチェックします。 結果は、表示用の配列で返されます。

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");
        isoStore.CreateFile("InTheRoot.txt");
        isoStore.CreateFile("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt");
        // 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.
}
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")
        isoStore.CreateFile("InTheRoot.txt")
        isoStore.CreateFile("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt")
        ' 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

こちらも参照ください