C#如何更新文件夹? [英] C# How to Update a folder?

查看:151
本文介绍了C#如何更新文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在程序文件中有一个名为旧"和新"的文件夹.

我想将文件从新"复制到旧". 新"中的文件和文件夹/子文件夹与旧"中的文件和文件夹/子文件夹相同,只是它们已被更新.

他们是这样做的一种简便方法,而不是我自己在移动新文件之前必须检查每个文件是否存在并删除它吗?

该程序将以管理员身份运行.

我有很多文件要删除/替换,然后分别键入每个文件,而将所有这些过程都放在一个按钮上,这将使我永远无法写作.是我可以将所有文件文件夹和子文件夹从新到旧移动并一次性移动和替换的一种方法吗?

这适用于Windows窗体应用程序的C#

I have a folder in program files called "Old" and "New".

I would like to copy files from "New" to "Old". The files and folders/subfolders in "New" Are the same as the ones in "Old" accept they are updated.

Is their an easy way to do this rather than myself having to check if each file exists and deleting it before moving the new files?

Program will be ran as administrator.

I have a lot of files i would like to delete/replace and typing each one out individually and having all this process on one button is taking me forever to write. Is their a way i can move all files folders and subfolders in New to Old and move and replace them all in one go?

This is for C# for Windows Forms App

推荐答案

您可以将
Directory.Move(sourcename,destname;

用于目录和文件

File.Move(sourcename, destname);


希望这能解决您的问题.


Hope this will solve your problem.


private static void CopyFilesRecursively(String SourcePath, String DestinationPath) 
{ 
   // First create all of the directories 
   foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))
      Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath)); 

   // Copy all the files 
   foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))
      File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath)); }



这可行,但是它说目录中的文件已经存在...但是我做到了,所以如果该文件夹存在,则在复制新文件夹之前将其删除



This works but it says a file in the directory already exists... But i made it so if the folder exists it is deleted before i copy the new folder


这篇关于C#如何更新文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆