c# - 您如何获得 IsolatedStorage 中所有文件的平面列表?

标签 c# .net isolatedstorage isolatedstoragefile

我需要获取给定 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;

    }

}

关于c# - 您如何获得 IsolatedStorage 中所有文件的平面列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3038770/

相关文章:

c# - 构建模块化的 asp 核心 Angular 应用程序

c# - "Due to heavy load, the latest workflow operation has been queued"--> 如何查看队列?

c# - 启动画面 Activity 强制我的设备重新启动

c# - 等待全部限制

c# - 银光 : Sterling Throws Exception

C# - 将对象保存到 JSON 文件

c# - 具有空值的 ASP.NET MVC 属性路由

c# - 当客户端连接到 WCF 服务时观察超时异常 - 两者都驻留在同一应用程序中

.net - 如何在.NET程序集中安全存储加密 key

javascript - 独立存储中 HTML 中的 JS/CSS 加载问题