我需要在一个给定的IsolatedStorage文件中的所有文件的列表。有关闭IsolatedStorage和这些需要的根子文件夹被包括在列表中。

通常System.IO类不能与IsolatedStorage使用。

有帮助吗?

解决方案

下面是我想出来的 - 它的工作原理,但如果有更好的选择我很想看到:

using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;

public static class IsolatedStorageFileExtensions
{

    /// <summary>
    /// Recursively gets a list of all files in isolated storage
    /// </summary>
    /// <remarks>Based on <see cref="http://dotnetperls.com/recursively-find-files"/></remarks>
    /// <param name="isolatedStorageFile"></param>
    /// <returns></returns>
    public static List<string> GetAllFilePaths(this IsolatedStorageFile isolatedStorageFile)
    {
        // Store results in the file results list
        List<string> result = new List<string>();

        // Store a stack of our directories
        Stack<string> stack = new Stack<string>();

        // Add initial directory
        string initialDirectory = "*";
        stack.Push(initialDirectory);

        // Continue while there are directories to process
        while (stack.Count > 0)
        {
            // Get top directory
            string dir = stack.Pop();

            string directoryPath;
            if (dir == "*")
            {
                directoryPath = "*";
            }
            else
            {
                directoryPath = dir + @"\*";
            }

            // Add all files at this directory to the result List
            var filesInCurrentDirectory = isolatedStorageFile.GetFileNames(directoryPath).ToList<string>();

            List<string> filesInCurrentDirectoryWithFolderName = new List<string>();

            // Prefix the filename with the directory name
            foreach (string file in filesInCurrentDirectory)
            {
                filesInCurrentDirectoryWithFolderName.Add(Path.Combine(dir, file));
            }

            result.AddRange(filesInCurrentDirectoryWithFolderName);

            // Add all directories at this directory
            foreach (string directoryName in isolatedStorageFile.GetDirectoryNames(directoryPath))
            {
                stack.Push(directoryName);
            }

        }

        return result;

    }

}

其他提示

你是天才,但也检索你要他们的道路与初始目录相结合的新目录时。

// Add all directories at this directory
foreach (string directoryName in isolatedStorageFile.GetDirectoryNames(directoryPath))
{
    stack.Push(Path.Combine(dir,directoryName));
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top