我刚刚学习 C#(已经摆弄它大约 2 天了),我决定,出于学习目的,我将重建一个用 VB6 制作的旧应用程序,用于同步文件(通常通过网络)。
当我在 VB 6 中编写代码时,它的工作原理大致如下:
- 创建一个 Scripting.FileSystemObject
- 为源和目标创建目录对象
- 为源和目标创建文件列表对象
- 迭代源对象,并检查目的地是否存在
- 如果没有,请创建它
- 如果是,请检查源版本是否更新/更大,如果是,则覆盖另一个
到目前为止,这就是我所拥有的:
private bool syncFiles(string sourcePath, string destPath) {
DirectoryInfo source = new DirectoryInfo(sourcePath);
DirectoryInfo dest = new DirectoryInfo(destPath);
if (!source.Exists) {
LogLine("Source Folder Not Found!");
return false;
}
if (!dest.Exists) {
LogLine("Destination Folder Not Found!");
return false;
}
FileInfo[] sourceFiles = source.GetFiles();
FileInfo[] destFiles = dest.GetFiles();
foreach (FileInfo file in sourceFiles) {
// check exists on file
}
if (optRecursive.Checked) {
foreach (DirectoryInfo subDir in source.GetDirectories()) {
// create-if-not-exists destination subdirectory
syncFiles(sourcePath + subDir.Name, destPath + subDir.Name);
}
}
return true;
}
我读过似乎提倡使用FileInfo 或 DirectoryInfo 对象对“Exists”属性进行检查,但我专门寻找一种搜索现有文件集合/列表的方法,而不是对每个文件的文件系统进行实时检查,因为我将这样做通过网络并不断返回到数千个文件的目录是很慢很慢的。
提前致谢。
I am just learning C# (have been fiddling with it for about 2 days now) and I've decided that, for leaning purposes, I will rebuild an old app I made in VB6 for syncing files (generally across a network).
When I wrote the code in VB 6, it worked approximately like this:
- Create a
Scripting.FileSystemObject
- Create directory objects for the source and destination
- Create file listing objects for the source and destination
- Iterate through the source object, and check to see if it exists in the destination
- if not, create it
- if so, check to see if the source version is newer/larger, and if so, overwrite the other
So far, this is what I have:
private bool syncFiles(string sourcePath, string destPath) {
DirectoryInfo source = new DirectoryInfo(sourcePath);
DirectoryInfo dest = new DirectoryInfo(destPath);
if (!source.Exists) {
LogLine("Source Folder Not Found!");
return false;
}
if (!dest.Exists) {
LogLine("Destination Folder Not Found!");
return false;
}
FileInfo[] sourceFiles = source.GetFiles();
FileInfo[] destFiles = dest.GetFiles();
foreach (FileInfo file in sourceFiles) {
// check exists on file
}
if (optRecursive.Checked) {
foreach (DirectoryInfo subDir in source.GetDirectories()) {
// create-if-not-exists destination subdirectory
syncFiles(sourcePath + subDir.Name, destPath + subDir.Name);
}
}
return true;
}
I have read examples that seem to advocate using the FileInfo or DirectoryInfo objects to do checks with the "Exists" property, but I am specifically looking for a way to search an existing collection/list of files, and not live checks to the file system for each file, since I will be doing so across the network and constantly going back to a multi-thousand-file directory is slow slow slow.
Thanks in Advance.
发布评论
评论(2)
GetFiles()
方法只会获取确实存在的文件。它不会随机生成不存在的文件。所以你所要做的就是检查它是否存在于另一个列表中。以下内容可能有效:
注意:您当然不能保证在调用
后某些内容没有发生变化GetFiles()。例如,如果您稍后尝试复制文件,则该文件可能已被删除或重命名。
通过使用
Except
方法或类似的方法。例如这样的事情:FileNameComparer 的实现方式如下:
虽然未经测试:p
The
GetFiles()
method will only get you files that does exist. It doesn't make up random files that doesn't exist. So all you have to do is to check if it exists in the other list.Something in the lines of this could work:
Note: You have of course no guarantee that something hasn't changed after you have done the calls to
GetFiles()
. For example, a file could have been deleted or renamed if you try to copy it later.Could perhaps be done nicer somehow by using the
Except
method or something similar. For example something like this:Where the FileNameComparer is implemented like so:
Untested though :p
一个小细节,
我会使用
Path 对文件名和文件夹名进行可靠的、独立于操作系统的操作。
我还注意到
optRecursive.Checked
不知从何而来。作为良好设计的一个问题,请将其作为一个参数:既然您提到它可能用于大量文件,请留意 .NET 4,它有一个 IEnumerable 替代 GetFiles() ,可以让您处理这个以流媒体的方式。
One little detail, instead of
I would use
Path does reliable, OS independent operations on file- and foldernames.
Also I notice
optRecursive.Checked
popping out of nowhere. As a matter of good design, make that a parameter:And since you mention it may be used for large numbers of files, keep an eye out for .NET 4, it has an IEnumerable replacement for GetFiles() that will let you process this in a streaming fashion.