gpt4 book ai didi

c# - 将图像复制到另一个文件夹

转载 作者:行者123 更新时间:2023-11-29 23:51:22 25 4
gpt4 key购买 nike

我正在使用 Xamaring 表单,我正在尝试将选定的图像路径复制到智能手机上的另一个位置,但无法正常工作。知道为什么以及如何解决它吗?

  private async Task btn_AddImg_ClickedAsync(object sender, EventArgs e)
{
var file = await CrossFilePicker.Current.PickFileAsync();
if (file != null)
{
Error.IsVisible = true;
Error.Text = file.FilePath;

var dirToCreate = Path.Combine(Android.App.Application.Context.FilesDir.AbsolutePath, "WightLossPersonal");
if (!Directory.Exists(dirToCreate))
{

var x= Directory.CreateDirectory(dirToCreate);
System.IO.File.Copy(file.FilePath, dirToCreate, true);

}
else
{
System.IO.File.Copy(file.FilePath, dirToCreate, true);
}

}
}

在我的 list 中我获得了权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

错误信息:

“/data/user/0/com.companyname.WightLoss/files/WightLossPersonal是一个目录”

enter image description here

最佳答案

您的主要问题是您没有在新目录中传递文件名。所以这就像你试图复制目录本身,而不是文件!

基本上你必须将文件名与目录结合起来然后将其传递给 Copy() 方法。

string destFolder = Path.Combine(dirToCreate, file.Name);
System.IO.File.Copy(file.FilePath, destFolder , true);

但让我们让代码更简洁。我会评论代码以便更好地理解。

private async Task btn_AddImg_ClickedAsync(object sender, EventArgs e)
{
var file = await CrossFilePicker.Current.PickFileAsync();
if (file != null)
{
Error.IsVisible = true;
Error.Text = file.FilePath;

var dirToCreate = Path.Combine(Android.App.Application.Context.FilesDir.AbsolutePath, "WightLossPersonal");
if (!Directory.Exists(dirToCreate))
{
Directory.CreateDirectory(dirToCreate);
// var x= Directory.CreateDirectory(dirToCreate); // don't need that variable x here since you don't want to use it later
//System.IO.File.Copy(file.FilePath, dirToCreate, true); No need here, will copy it in all ways down .

}
//else // you don't need else, copy the file when finishing the check.
//{

// Make a new path to compine the dir and the fileName
string destFolder = Path.Combine(dirToCreate, file.Name);
System.IO.File.Copy(file.FilePath, destFolder , true);
//}

}


}

关于c# - 将图像复制到另一个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50883031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com