小编Bib*_*ins的帖子

Xamarin.Forms 在文件系统中保存文件

我尝试从网络下载文件并保存到任何 Environment.SpecialFolder 位置。无论我尝试什么方法 - 我总是得到

System.UnauthorizedAccessException: '访问路径'我尝试的任何可能的路径(甚至超出 Environment.SpecialFolder)'被拒绝。

我尝试在 UWP 和 Android 上访问文件系统 - 都是相同的例外。

到目前为止我没有运气的尝试:

  • 我尝试通过 PCL 和使用 DependencyService 分别通过每个平台来做到这一点。
  • 检查文件夹是否为只读
  • 以管理员权限启动 Visual Studio
  • 将调试更改为发布
  • 试过 Xam.Plugins.DownloadManager

  • 代码示例:

    var webClient = new WebClient();
    webClient.DownloadDataCompleted += (s, e) => {
       var bytes = e.Result;
       string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
       string localFilename = "downloaded.jpg";
       string localPath = Path.Combine(documentsPath, localFilename);
       File.WriteAllBytes(localPath, bytes);
    };
    webClient.DownloadDataAsync(new Uri(url));
    
    Run Code Online (Sandbox Code Playgroud)

    c# filesystems android xamarin.forms uwp

    7
    推荐指数
    1
    解决办法
    1万
    查看次数

    标签 统计

    android ×1

    c# ×1

    filesystems ×1

    uwp ×1

    xamarin.forms ×1