【问题标题】:PCLStorage - How to get path to writable folder?PCLStorage - 如何获取可写文件夹的路径?
【发布时间】:2017-03-24 23:57:09
【问题描述】:

我需要下载并保存文件。我使用此代码(使用 Xam.Plugins.DownloadManager 和 PCLStorage 库):

public static string DownloadNewXml(string LinkToFile, string PathFile)
    {
        var downloadManager = CrossDownloadManager.Current;
        CrossDownloadManager.Current.PathNameForDownloadedFile = new System.Func<IDownloadFile, string>(file => {
           return PathFile;
        });
        try
        {
            var file = downloadManager.CreateDownloadFile(LinkToFile);
            downloadManager.Start(file);
            return "ok";
        }
        catch(Exception e)
        {
            return e.ToString() ;
        }
    }

PathFile 是:FileSystem.Current.LocalStorage.Path + "file.xml"; 此代码在 Android 和 UWP 上引发异常 - 我无法将文件写入此路径 (FileSystem.Current.LocalStorage.Path)。所以我有一个问题。如何在所有平台(Android、UWP、iOS)上为我的应用程序获取可写文件夹的路径?请帮忙。

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    尝试改用Path.Combine(FileSystem.Current.LocalStorage.Path, "file.xml")

    【讨论】:

    • 它适用于 UWP。在 Android 上抛出异常:zapodaj.net/93c843db31fd5.png.html 谢谢你的帮助 :)
    • 知道了。要使用 Android 存储进行写入,您必须使用外部存储路径。您可以通过 DependencyService 从 Android 项目中获取它。此代码将返回您的本地外部存储应用程序路径:Android.App.Application.Context.GetExternalFilesDir(null)。它会返回类似/mnt/sdcard/Android/data/com.yourcompany.appname
    • 它正在工作!非常感谢!顺便提一句。为什么它没有在 PCLStorage 中实现?
    • 这是非常特定于平台的。例如,iOS 根本没有外部存储。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 2011-05-08
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多