如何将文件从独立存储复制到下载文件夹? [英] How can I copy a file from the isolated storage to the Downloads folder?

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

问题描述

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

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
  • 你最终会得到类似的结果:

    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);
    }
    

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

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