用 C# 制作一个备份实用程序
我正在考虑将备份实用程序作为我的下一个项目想法,但我真的不知道从哪里开始或应该如何进行备份。
任何人都可以阐明归档备份和恢复等的方法是怎样的。
谢谢
I'm thinking of a Backup Utility as my next project idea, but I don't really know where to start or how the backups should be made.
Can anyone shine some light on what methods of archiving backups and restoring etc. is done.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,我不认为我在 .NET Framework 中见过任何专门为备份/存档/恢复活动设计的命名空间或类。如果有人知道的话,请赐教!
最简单的说,备份文件就是将文件从一个位置复制到另一个位置。假设您有想要维护备份的源代码,并且它每天都会发生一些变化。您可以创建一个小型控制台应用程序,只需将目标文件夹中的所有文件复制到外部驱动器,覆盖自上次复制操作以来更改的所有文件,并将任何新文件和文件夹添加到目标。然后使用计划任务每天在不使用计算机时运行该实用程序一次,或者至少在不编辑源代码时运行该实用程序。
System.IO 命名空间中有很多方法和类,这使得这件事相对容易做到。
IO 命名空间中的一些有用的类是:
下面是一个代码示例,它将为您提供 C: 驱动器根目录下所有顶级文件夹的数组:
To start with, I don't believe I've seen any namespace or classes in the .NET Framework specifically designed for backup/archive/restore activities. If anyone knows of any, well, enlighten us please!
At its simplest, backing up files is simply copying files from one location to another. Let's say that you have source code that you would like to maintain a backup of, and it changes a bit day by day. You could create a small console app that would simply copy all the files in the target folders to an external drive, overwriting any files that had changed since the last copy operation, and adding any new files and folders to the destination. Then use a Scheduled Task to run the utility once per day at a time when the computer is not being otherwise used -- or at least when the source is not being edited.
There are lots of methods and classes in the System.IO namespace that will make this relatively easy to do.
Some useful classes in the IO namespace are:
Here's a code sample that would give you an array of all the top-level folders just under the root of the C: drive:
AlphaVSS 向 .NET 代码公开完整的 VSS(卷影复制服务)API。这可以让您使用托管代码创建某个时间点的快照。
AlphaVSS exposes the full VSS (Volume Shadow Copy Service) API to .NET code. This could allow you to create a snapshot of a point in time using managed code.
更简单的方法之一是:
如果您只想复制文件,那么您可以使用DirectoryInfo和FilesInfo获取目录、文件,并执行FileCopy、FileMove等...
如果你想在目的地压缩文件,那么你可以使用现有的库,如7z,它也有C#代码。
One of easier methods would be:
If you just want to copy files, then you can use DirectoryInfo and FilesInfo to obtain directory, files, and perform FileCopy, FileMove, etc...
If you want to compress files at destination, then you can use the existing library like 7z, which has codes for C# as well.
通读这篇维基百科文章 - 备份并确保您了解其中的所有内容。
最简单的备份实用程序是允许用户选择源文件夹和目标文件夹,然后将其复制。 (因为这就是备份的本质)。
但是您将需要一些递归文件夹调用来备份文件夹内的每个文件夹。
Read through this Wikipedia Article - Backup and make sure you understand everything in it.
The simplest backup utility would be to allow a user to select a source folder, and destination folder, and just copying it across. (as that is all a backup is essentially).
But you will need some recursive folder calls to backup each folder inside folders.