PCL 存储包不创建文件夹
本文关键字:文件夹 创建 包不 存储 PCL | 更新日期: 2023-09-27 18:36:01
我已经使用 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"文件夹。
我已经为我的安卓项目设置了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).我从优秀的人那里学到了这一点
MSDN 最佳实践文章关于异步/等待。
以前,我在方法不创建目录或文件(如您的问题)或线程挂起之间来回切换。本文详细讨论了后者。
ResourceLoader 类来自这里:
嵌入式资源
ResourceToFile() 方法只是一个帮助程序,它将 iOS 中的长资源名称转换为短文件名,因为我更喜欢这些名称。这里不是生殖器(IOW:这是一个笨拙的笨拙,我很惭愧地向;)
我想我一天比一天更好地理解线程,如果我理解正确,这里的艺术是确保您等待加载和写入文件的异步方法完成,但请确保您在不会与主 UI 线程死锁的线程池上执行此操作。