C# PCL存储包不创建文件夹

C# PCL存储包不创建文件夹,c#,xamarin.forms,C#,Xamarin.forms,我已经使用PCL存储包为我的应用程序创建了一个文件夹。我提到。以下是我的代码示例: public ListPage() { testFile(); Content = new StackLayout { Children = { new Label { Text = "Hello ContentPage" }

我已经使用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/directory下创建的,但不会在files下创建“MySubFolder”文件夹


我已经为我的android项目设置了WRITE_EXTERNAL_存储和READ_EXTERNAL_存储。我是否缺少任何其他配置

我也遇到过类似的问题(虽然是在iOS上),现在我有了这个解决方案,也许它对你有帮助。问题在于如何正确处理异步调用和其他线程乐趣

首先,我的使用案例是,我将许多文件资源与应用捆绑在一起,这些文件资源在第一次运行时提供给用户,但从那时起,将在线更新。因此,我将bundle资源复制到文件系统中:

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)。我从优秀的老师那里学到了这一点

之前,我在不创建目录或文件的方法(如您的问题所示)和挂起的线程之间来回切换。本文详细论述了后者

ResourceLoader类来自以下位置:

ResourceToFile()方法只是一个助手,它可以将iOS中的长资源名转换为短文件名,我更喜欢这样。这里不是日尔曼(IOW:这是一个我羞于展示的乱七八糟的东西;)

我想我一天比一天更了解线程,如果我理解正确的话,这里的技巧是确保您等待加载和写入文件的异步方法完成,但确保您在不会与主UI线程死锁的线程池上执行此操作