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

标签 c# windows file xaml copy

我在处理文件时遇到问题。我需要复制一个 .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/

相关文章:

c - 在 C 中打印出一个字符串数组

java - 如何在Java中读取Delphi记录结构

file - 如何处理 wix 源路径中的空格?

C# wpf 问题自定义复选框

c# - 如何在列表框 WP8 中绑定(bind)数据用户控件

c# - Json字符串反序列化为对象数组列表

windows - 在 linux 上编译 windows 屏保

c# - 将单元格内容复制到剪贴板 RadGridView WPF

linux - 从 Windows 调用基于 LINUX 的二进制文件并返回输出

windows - 在 Batch 中显示变量的问题