确保使用 c# 成功复制文件

发布于 2024-11-09 04:39:47 字数 669 浏览 0 评论 0原文

我正在创建一个应用程序来将文件从源目录备份到目标目录。我将源文件夹和目标文件夹中的文件信息存储在单独的列表中,然后根据列表的大小、修改日期等对列表进行比较,以查看需要复制哪些文件。

无论如何,重点是我最终得到了需要复制的文件列表,我想知道每个文件花费了多少时间,因此我尝试了以下技术:

技术 1

技术 2

技术 3:常规 File.Copy("source....","Destination")

前两种技术是很棒,因为我可以看到进步。问题是,当我使用这些技术复制一些文件时,新文件有时会有不同的日期。我希望这两个文件具有相同的修改日期和相同的创建日期。此外,如果出于某种原因我的程序崩溃,正在复制的文件将被损坏,因为我尝试复制一个大文件(在 Windows 中复制需要大约一分钟的文件),如果我在复制文件的同时退出程序正在复制的文件有时具有相同的属性和相同的大小,因此我想确保我的文件没有损坏,以防程序崩溃。

也许我应该使用以太技术 1 或 2,然后最后从源文件复制属性并将它们分配给目标文件。但我不知道该怎么做。

I am creating an application to back up files from a source directory into a destination directory. I store the files information from the source and destination folders in separate lists then I compare the lists based on their size, date modified etc to see which files need to be copied.

Anyways the point is that I end up with a list of the files that need to be copied and I will like to know how much time is every file taking therefore I have tried the following techniques:

Technique 1

Technique 2

Thechnique 3 : the regular File.Copy("source....","Destination")

The first two techniques are great because I can see the progress. The problem is that when I copy some files with those techniques, the new file sometimes has different dates. I will like both files to have the same modified date and also the same creation date. Moreover if for whatever reason my program crashes the file that is being copied will be corrupted because I have tried copying a large file ( a file that takes about a minute to get copied in windows) if I exit my program meanwhile the file is being copied the file that is being copied sometimes has the same attributes and the same size so I want to make sure I don't have corrupted files in case my program crashes.

Maybe I should use aether techniques 1 or 2 and then at the end copy the attributes from the source file and assign those to the destination file. I don't know how to do that though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度℉ 2024-11-16 04:39:47

FileInfo 具有可设置的成员 CreationTime 和 LastWriteTime - 因此如果有帮助的话,您可以选择自己喜欢的技术,然后再设置日期。

FileInfo has members CreationTime and LastWriteTime that are settable - so you could settle for your preferring techniques and set the dates afterwards if that helps.

话少情深 2024-11-16 04:39:47

您是否考虑过编写一个调用 robocopy 的 shell 脚本?每当我必须运行这样的备份任务时,我只需编写一个脚本 - robocopy 已经为我完成了繁重的工作,因此通常不需要创建定制应用程序。

Have you considered just writing a shell script that calls robocopy? Any time I've had to run backup tasks like this, I just write a script -- robocopy already does the heavy lifting for me, so there's often no need to create a bespoke application.

删除会话 2024-11-16 04:39:47

我有一个解决方案,但它很长:

我知道我可以从源复制文件,然后在目标中将文件命名为其他名称,例如“fileHasNotBeenCopiedYet”,并具有隐藏属性,然后当我的程序完成复制文件时,将名称更改为源名称并复制属性,然后我知道如果存在具有该名称的文件(“fileHasNotBeenCopiedYet”),则意味着该文件已损坏。

A solution that I have but its long:

I know I can copy the file from the source and then name the file in the destination something else like "fileHasNotBeenCopiedYet" with attributes of hidden then when my program finishes copying the file change the name to the source name and copy the attributes and then latter I know that if a file with that name ("fileHasNotBeenCopiedYet") exists that means that the file is corrupted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文