在Xamarin.Forms中,如何将文件从隔离存储复制到“下载"文件夹? [英] In Xamarin.Forms, how can I copy a file from the isolated storage to the Downloads folder?

查看:328
本文介绍了在Xamarin.Forms中,如何将文件从隔离存储复制到“下载"文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据库文件从隔离存储区复制到下载"文件夹(或用户可以访问的任何文件夹).

I'm trying to copy my database file from the isolated storage to the Download folder (or any folder that the user can access).

当前我的数据库存储在:

Currently my database is stored in:

/data/user/0/com.companyname.appname/files/Databases/MyDatabase.db

我尝试使用此代码:

public string GetCustomFilePath(string folder, string filename)
{
    var docFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    var libFolder = Path.Combine(docFolder, folder);

    if (!Directory.Exists(libFolder))
        Directory.CreateDirectory(libFolder);

    return Path.Combine(libFolder, filename);
}


var bas = GetDatabaseFilePath("MyDatabase.db");
var des = Path.Combine(Android.OS.Environment.DirectoryDownloads, "MyDatabase.db");
File.Copy(bas, des);

Android.OS.Environment.DirectoryDownloads属性返回路径Download,这是下载文件夹的名称.
但是File.Copy()抛出一个异常告诉

The Android.OS.Environment.DirectoryDownloads property returns the path Download, which is the name of the downloads folder.
But File.Copy() throws an exception telling

System.IO.DirectoryNotFoundException:找不到目标目录: 下载.

System.IO.DirectoryNotFoundException: Destination directory not found: Download.

我曾经尝试过这样的斜杠:/Download/MyDatabase.db没有运气.

I tried to use a slash before like this: /Download/MyDatabase.db with no luck.

有没有办法复制这样的文件?我需要任何许可吗?

Is there any way to copy a file like that? Do I need any permission?

推荐答案

1st)是,您确实需要权限才能写入外部存储.

1st) Yes, you do need permissions to write to external storage.

您可以自行获得所需的运行时间许可:

You can get the runtime time permission required by doing it yourself:

或通过第三方插件,例如James Montemagno的PermissionsPlugin

Or via a 3rd-party plugin, such as James Montemagno's PermissionsPlugin

2nd)一旦用户接受可以写入外部存储的操作,就可以使用:

2nd) Once your user accepts that it is ok to write to external storage, you can use:

Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads

要获取设备公共下载文件夹的路径,即使用Forms的依赖项服务,请执行以下操作:

To obtain the path of the device's public Download folder, i.e. using a Forms' dependency service:

public interface IDownloadPath
{
    string Get(); 
}

public class DownloadPath_Android : IDownloadPath
{
    public string Get()
    {
        return Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
    }
}

  • https://docs .microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction
    • https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction
    • 您最终得到类似的东西:

      You end up with something like:

      public void Handle_Button(object sender, System.EventArgs e)
      {
          var fileName = "someFile.txt";
          using (var stream = File.Create(Path.Combine(FileSystem.CacheDirectory, fileName)))
          {
             // just creating a dummy file to copy (in the cache dir using Xamarin.Essentials
          }
      
      
          var downloadPath = DependencyService.Get<IDownloadPath>().Get();
          File.Copy(Path.Combine(FileSystem.CacheDirectory, fileName), downloadPath);
      }
      

      这篇关于在Xamarin.Forms中,如何将文件从隔离存储复制到“下载"文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆