gpt4 book ai didi

c# - 将文件复制到新文件夹中

转载 作者:可可西里 更新时间:2023-11-01 11:28:40 26 4
gpt4 key购买 nike

我在处理文件时遇到问题。我需要复制一个 .db 文件并将其放入一个新文件夹(称为“目录”,之前使用 FolderPicker 选择)。我的代码是:(这是用于 Windows 8.1 的商店应用程序)

try{
StorageFile newDB = await StorageFile.GetFileFromPathAsync(directory);
StorageFile originalDB = await StorageFile.GetFileFromPathAsync(Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "AFBIT.db"));
await newDB.CopyAndReplaceAsync(originalDB);
}
catch(Exception ex){
}

我在neDB有异常,说“Value does not fall in the expected range”。我不知道在 xaml 中复制文件的另一种方法,如果您知道问题所在或其他方法,我将不胜感激。

最佳答案

我有一些类似的东西,我目前在复制文件时使用 CopyFileAsync 我创建的方法看看这是否可以帮助您将代码重构为工作模型

public static async Task CopyFileAsync(string sourcePath, string destinationPath)
{
try
{
using (Stream source = File.Open(sourcePath, FileMode.Open))
{
using (Stream destination = File.Create(destinationPath))
{
await source.CopyToAsync(destination);
}
}
}
catch (IOException io)
{
HttpContext.Current.Response.Write(io.Message); //I use this within a web app change to work for your windows app
}
}

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

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