C#如何更新文件夹?

编程入门 行业动态 更新时间:2024-10-23 06:22:29
本文介绍了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#如何更新文件夹?

本文发布于:2023-10-30 19:26:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543777.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!