我需要获取文件夹(广告子文件夹)内所有文件的文件详细信息。我已经为此写了一个递归函数。在递归函数中没有得到更新的值Windows Phone 8
private async Task getallfiles(StorageFolder appfolder)
{
IReadOnlyList<StorageFile> sortedItems1 = await appfolder.GetFilesAsync();
if (sortedItems1.Count > 0)
{
foreach (StorageFile file in sortedItems1)
CopyContentToIsolatedStorage(file.Path);
}
IReadOnlyList<StorageFolder> sorteditems2 = await appfolder.GetFoldersAsync();
if (sorteditems2.Count > 0)
{
foreach (StorageFolder folder in sorteditems2)
await getallfiles(folder);
}
}
现在,当我打电话与作为参数传递的根文件夹这个功能,我正在里面sorteditems根文件夹,这是一个全局变量只有文件。我尝试传递不同的文件夹作为参数,但每次返回的已排序项只包含父文件夹中的文件传递,而不包含子文件夹中的文件。
我是否错过了某些东西,或者是否存在某些代码的逻辑错误。任何帮助,将不胜感激。
例外我得到 - 它可能找到问题的帮助:
{System.NullReferenceException:对象不设置到对象的实例。 在Appzillon.MainPage.d__3d.MoveNext() ---从先前的位置在那里引发异常--- 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务task) 堆栈跟踪System.Runtime结束。 CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Appzillon.MainPage.d__36.MoveNext() ---从之前位置抛出异常的堆栈跟踪结束 - - at System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__0(Object state)}。
而且功能CopyContentToIsolatedStorage如下:
public static void CopyContentToIsolatedStorage(string file)
{
// Obtain the virtual store for the application.
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
if (iso.FileExists(file))
return;
var fullDirectory = System.IO.Path.GetDirectoryName(file);
if (!iso.DirectoryExists(fullDirectory))
iso.CreateDirectory(fullDirectory);
// Create a stream for the file in the installation folder.
using (Stream input = Application.GetResourceStream(new Uri(file, UriKind.Relative)).Stream)
{
// Create a stream for the new file in isolated storage.
using (IsolatedStorageFileStream output = iso.CreateFile(file))
{
// Initialize the buffer.
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
// Copy the file from the installation folder to isolated storage.
while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
output.Write(readBuffer, 0, bytesRead);
}
}
}
}
编辑:更新的代码,增加了我得到的例外。我只得到根文件夹文件的原因是因为我已经将sortedItems作为Readonly,正如kennyzx所提到的。但即使尝试了他的修改后的代码,并且实际上也将其更改为上述更新的代码,但我仍然遇到问题。请参阅我的意见kennyzx的答案...
问候
感谢您的回答。就在我看到你的答案之前,我在某处阅读了有关异步任务的内容,并将我的功能更改为异步任务,并添加了等待。但是,当你猜对的时候,我对已分类项目的声明是错误的。但是,尽管发生了这些变化,代码仍然处于两者之间。由于我的实际动机是将文件复制到独立存储,我为此写了一个函数CopyContentToIsolatedStorage(StorageFolder文件夹)。我决定用sortedItems1列表中的foreach函数替换AddRange(以前的concat)语句和上面的函数。 cond ... – 2014-11-24 12:42:35
新替换的代码如下所示:'if(sortedItems1.Count> 0) { foreach(StorageFile file in sortedItems1) CopyContentToIsolatedStorage(file.Path); }而不是'sortedItems.AddRange(sortedItems1);'即使在这之后,代码也被打破了同样的错误。但我似乎已经理解的一件事是,它是一个异步问题,只是在应用程序崩溃之后,当我检查应用程序的隔离存储,复制的文件以及... contd ... – 2014-11-24 12:42:58
您可以编辑自己的问题使用新的代码,并发布'CopyContentToIsolatedStorage'的实现。 – kennyzx 2014-11-24 12:47:44