用 C# 制作一个备份实用程序

发布于 2024-09-16 03:41:42 字数 97 浏览 6 评论 0原文

我正在考虑将备份实用程序作为我的下一个项目想法,但我真的不知道从哪里开始或应该如何进行备份。

任何人都可以阐明归档备份和恢复等的方法是怎样的。

谢谢

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 技术交流群。

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

发布评论

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

评论(5

青春有你 2024-09-23 03:41:42

首先,我不认为我在 .NET Framework 中见过任何专门为备份/存档/恢复活动设计的命名空间或类。如果有人知道的话,请赐教!

最简单的说,备份文件就是将文件从一个位置复制到另一个位置。假设您有想要维护备份的源代码,并且它每天都会发生一些变化。您可以创建一个小型控制台应用程序,只需将目标文件夹中的所有文件复制到外部驱动器,覆盖自上次复制操作以来更改的所有文件,并将任何新文件和文件夹添加到目标。然后使用计划任务每​​天在不使用计算机时运行该实用程序一次,或者至少在不编辑源代码时运行该实用程序。

System.IO 命名空间中有很多方法和类,这使得这件事相对容易做到。

IO 命名空间中的一些有用的类是:

  • Directory 提供用于创建、移动和枚举目录和子目录的静态方法
  • DirectoryInfo 是公开文件夹/目录属性的类
  • File 为文件提供静态方法
  • FileInfo 是公开文件属性的类。

下面是一个代码示例,它将为您提供 C: 驱动器根目录下所有顶级文件夹的数组:

string[] d = Directory.GetDirectories(@"C\", "*.*", SearchOption.TopDirectoryOnly);
DirectoryInfo[] di = new DirectoryInfo[d.Length];
for (int x = 0; x < d.Length; x++)
{
    di[x] = new DirectoryInfo(d[x]);
}

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:

  • Directory provides static methods for creating, moving and enumerating through directories and subdirecties
  • DirectoryInfo is a class exposing properties of Folders/Directories
  • File provides static methods for files
  • FileInfo is a class exposing properties of files.

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:

string[] d = Directory.GetDirectories(@"C\", "*.*", SearchOption.TopDirectoryOnly);
DirectoryInfo[] di = new DirectoryInfo[d.Length];
for (int x = 0; x < d.Length; x++)
{
    di[x] = new DirectoryInfo(d[x]);
}
薄暮涼年 2024-09-23 03:41:42

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.

万劫不复 2024-09-23 03:41:42

更简单的方法之一是:

  1. 允许用户选择目标文件夹和目的地以及目的地类型(Zip、复制等)
  2. 将目标文件夹内的文件和子文件夹复制/压缩到目的地

如果您只想复制文件,那么您可以使用DirectoryInfo和FilesInfo获取目录、文件,并执行FileCopy、FileMove等...

如果你想在目的地压缩文件,那么你可以使用现有的库,如7z,它也有C#代码。

One of easier methods would be:

  1. Allow user to select target folder and destination, and destination type (Zip, copy, etc)
  2. Copy/Compress the files and sub folders inside target folder to destination

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.

著墨染雨君画夕 2024-09-23 03:41:42
  • 您打算逐个扇区或逐个文件备份吗?
  • 您的备份将以管理员身份(访问所有文件)还是用户身份(仅访问某些文件)运行?
  • 如何保护现在存储在备份文件中的管理员级信息不被用户级查看者看到?
  • 您的目标介质必须有足够的空间用于备份(大型网络驱动器)还是备份能够跨越多个介质? (CDROM)
  • 在后一种情况下,您将如何处理大于单个目标介质的单个文件?
  • 您是否会始终执行完整备份(一切,始终)或增量备份(仅自上次备份以来发生的更改)或差异备份(仅自上次完整备份以来发生的更改)?
  • Are you planning on backing up sector-by-sector, or file-by-file?
  • Will your backup run as an Admin (access to all files) or as a User (access to only some files)?
  • How do you safeguard Admin-level information now stored in your backup file from User-level viewers?
  • Must your target medium have adequete space for the backup (large network drive) or will the backup be able to span several media? (CDROMs)
  • In the latter case, how will you deal with individual files bigger than a single target media?
  • Will you always do a full backup (everything, always) or an incremental (just what changes since the last backup) or a differential (just what changes since the last full backup)?
池木 2024-09-23 03:41:42

通读这篇维基百科文章 - 备份并确保您了解其中的所有内容。

最简单的备份实用程序是允许用户选择源文件夹和目标文件夹,然后将其复制。 (因为这就是备份的本质)。

但是您将需要一些递归文件夹调用来备份文件夹内的每个文件夹。

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.

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