PCL存储包不会创建文件夹
我使用PCL存储包为我的应用程序创建了一个文件夹。 我提到了这个 。 这是我的代码示例:
public ListPage() { testFile(); Content = new StackLayout { Children = { new Label { Text = "Hello ContentPage" } } }; } async public void testFile() { // get hold of the file system IFolder rootFolder = FileSystem.Current.LocalStorage; // create a folder, if one does not exist already IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder", CreationCollisionOption.OpenIfExists); // create a file, overwriting any existing file IFile file = await folder.CreateFileAsync("MyFile.txt", CreationCollisionOption.ReplaceExisting); // populate the file with some text await file.WriteAllTextAsync("Sample Text..."); }
文件夹是在sdcard / android / data /目录下创建的,但它不会在文件下创建“MySubFolder”文件夹。
我为我的android项目设置了WRITE_EXTERNAL_STORAGE和READ_EXTERNAL_STORAGE。 我错过了其他任何配置吗?
遇到类似的问题(虽然在iOS上),我现在有这个工作,也许它可以帮助你。 问题是正确处理异步调用和其他线程乐趣。
首先,我的用例是我将应用程序捆绑了许多文件资源,在第一次运行时为用户提供,但从那时起在线更新。 因此,我将捆绑资源并将其复制到文件系统中:
var root = FileSystem.Current.LocalStorage; // already run at least once, don't overwrite what's there if (root.CheckExistsAsync(TestFolder).Result == ExistenceCheckResult.FolderExists) { _testFolderPath = root.GetFolderAsync(TestFolder).Result; return; } _testFolderPath = await root.CreateFolderAsync(TestFolder, CreationCollisionOption.FailIfExists).ConfigureAwait(false); foreach (var resource in ResourceList) { var resourceContent = ResourceLoader.GetEmbeddedResourceString(_assembly, resource); var outfile = await _testFolderPath.CreateFileAsync(ResourceToFile(resource), CreationCollisionOption.OpenIfExists); await outfile.WriteAllTextAsync(resourceContent); }
注意.ConfigureAwait(false)。 我从优秀中学到了这一点
有关async / await的MSDN最佳实践文章 。
之前,我在不创建目录或文件的方法之间来回 – 如你的问题 – 或线程悬挂。 文章详细讨论了后者。
ResourceLoader类来自这里:
嵌入式资源
ResourceToFile()方法只是一个帮助器,它将iOS中的长资源名称转换为短文件名,因为我更喜欢这些。 这里不是绅士(IOW:这是一个让我感到羞耻的kludge;)
我认为我日复一日地理解线程,如果我理解正确,这里的艺术是确保你等待加载和写入文件的异步方法完成,但要确保你在不会死锁的线程池上这样做使用主UI线程。