如何使用带有Xamarin.Forms的C#从可移动SD卡获取文件副本到内部存储器?

时间:2017-07-17 13:17:01

标签: xamarin xamarin.android xamarin.forms

我正在尝试将文件从“可移动SD卡”(非外部)复制到内部存储,我已经多次调试代码,我看不到任何错误或异常被抛出。  这是我的代码:

private async void btnCopyFile_Clicked(object sender, EventArgs e)
    {
        var docs = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        try
        {
            //RemovableSdCardPath() gets the Path of removable storage
            var directories = Directory.GetFiles(RemovableSdCardPath(), "MyFile.sqlite", SearchOption.AllDirectories);
            if (File.Exists(directories[1]))
            {
                var myFile = directories[1];   // Output: "/storage/590B-3A12/database/"MyFile.sqlite"
                var destination = Path.Combine(docs, "MyFile.sqlite");   // Output: "/data/user/0/ESH_APP.ESH_APP/files/"MyFile.sqlite"
                var starttime = DateTime.UtcNow;
                File.Copy(myFile,destination);
                await DisplayAlert("Copied", (DateTime.UtcNow - starttime).TotalSeconds.ToString(), "Ok"); 
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

 Debug output:
<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/7OtEvS" frameborder="0"></iframe>

感谢。

0 个答案:

没有答案