问题
I need to get a list of all files in a given IsolatedStorage folder. There are sub folders off the root of the IsolatedStorage and these need to be included in the list.
The usual System.IO classes can't be used with IsolatedStorage.
回答1:
Here's what I've come up with - it works but I'd be interested to see if there are better alternatives:
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;
}
}
回答2:
You're genius but also when retrieving the new directories you have to combine their path with the initial directory.
// Add all directories at this directory
foreach (string directoryName in isolatedStorageFile.GetDirectoryNames(directoryPath))
{
stack.Push(Path.Combine(dir,directoryName));
}
来源:https://stackoverflow.com/questions/3038770/how-do-you-get-a-flat-listing-of-all-files-in-isolatedstorage