使用 .NET System.IO File.Copy 按顺序复制文件 [英] Copy file(s) by order using .NET System.IO File.Copy

查看:56
本文介绍了使用 .NET System.IO File.Copy 按顺序复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Windows 应用程序中创建一个基本的文件复制操作.我注意到 System.IO File.Copy 随机复制文件.有没有办法控制应该首先复制哪些文件.例如,如果我们要从文件大小从小到大复制文件.或者按字母顺序,假设开始复制文件名 [从] A 到 Z 的文件,或者按文件名 [从] 1 到 100 的数字顺序复制.

I'm creating a basic file copy operation in windows application. I noticed System.IO File.Copy copy files randomly. Is there a way to control what files should be copied first. For example If we want to copy files starting from smallest to largest file size. Or by Alphabetical order, lets say start copying files with filename [starting from] A to Z, or by numerical order with filename [from] 1 to 100.

我正在使用这个简单的代码从文件夹中复制文件,但这会随机"复制文件.见下文:

I'm using this simple code to copy files from a folder, but this copies files "randomly". See below:

Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
    Dim source as string = "c:\copyfiles"
    Dim destination as string = "d:\backup"
    Dim filepath as string = destination & "\"

        For Each filename As String In Directory.GetFiles(source)
            If File.Exists(filename) Then
                Dim dFile As String = String.Empty
                Dim dFilePath As String = String.Empty

                dFile = Path.GetFileName(filename)  'get filenames from source
                dFilePath = filepath & dFile  'concatenate filepath and filename

                File.Copy(filename, dFilePath, True) 'copy files from "c:\copyfiles" folder to destination
            End If

        Next
        MsgBox("Copy Successful", vbOKOnly, "Message")
End Sub

推荐答案

如果您确实想通过名称以外的其他方式处理它们(最小到最大文件大小,或者可能是最新或最旧),那么您应该使用 DirectoryInfo 以便您可以获取那些 FileInfo 属性.

If you do want to process them by something other than name (smallest to largest file size, or perhaps newest or oldest), then you should use DirectoryInfo so you can get at those FileInfo properties.

' simple ordering by size
Dim dix As New DirectoryInfo(_your_file_path)
For Each f As FileInfo In dix.EnumerateFiles.
               OrderByDescending(Function(o) o.Length)
    ' do stuff
Next

如果您认为您可能还需要过滤器(即只复制自上次运行以来的文件),那么 EnumerateFiles 而不是 GetFiles() 和一些 linq 将效率更高.在这种情况下,.NET 将评估您的过滤器,并只返回与您的过滤器匹配的过滤器,而不是所有的过滤器,供您在代码中手动排除:

If you think you might also need filters (ie just copy the files since the last time it ran) then, EnumerateFiles rather than GetFiles() with some linq will be more efficient. In this case, .NET will evaluate your filter and only return the ones matching your filter(s) rather than all of them for you to exclude manually in code:

' process only TXT files in order of size
For Each f As FileInfo In dix.EnumerateFiles.
                Where(Function(w) w.Name.EndsWith(".txt")).
                OrderByDescending(Function(o) o.Length)
    ' do stuff
Next

这篇关于使用 .NET System.IO File.Copy 按顺序复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆