将文件复制到新文件夹中

我有一个应对文件的问题。 我需要复制一个.db文件,并将其放在一个新的文件夹(称为“目录”,以前用FolderPickerselect)。 我有的代码是:(这是为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有个例外,说“价值不在预期范围内”。 我不知道另一种方法来复制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 } } 

我不确定你真正的问题,但我相信你的尝试是:

 public static bool CopyFile(string source, string destination) { if(!File.Exist(source)) return false; if(string.IsNullOrEmpty(destination)) return false; try { using(var reader = File.Open(source)) using(var writer = File.Create(destination)) reader.CopyTo(writer); return true; } catch(IOException ex) { return false; } } 

不要忘记,这会吃掉你的例外,如果因任何原因在任何时候失败,就会return false

这将本质上复制文件,我注意到你试图读取您的本地应用程序文件夹。 请小心,因为当它驻留在操作系统中的多个位置时,通常需要管理员权限