문제

한 폴더에서 다른 폴더로 많은 파일을 복사해야합니다. 현재 나는 이런 식으로하고있다 :

string[] files = Directory.GetFiles(rootFolder, "*.xml");
foreach (string file in files)
{
    string otherFile = Path.Combine(otherFolder, Path.GetFileName(file));
    File.Copy(file, otherFile);
}

그것이 가장 효율적인 방법입니까? 나이가 걸리는 것 같습니다.

편집 : 개별 파일을 복사하는 대신 배치 사본을 수행하는 더 빠른 방법이 있는지 묻고 있지만 답은 아니오라고 생각합니다.

도움이 되었습니까?

해결책

나는 file.copy보다 더 효율적인 방법을 생각할 수 없으며 OS로 직접 이동합니다.

반면에 그렇게 오래 걸리면 진행 대화 상자를 보여줄 것을 강력히 제안합니다. shfileoperation 당신을 위해 그것을합니다. 적어도 사용자는 무슨 일이 일어나고 있는지 알게 될 것입니다.

다른 팁

운영 체제를 사용하여 파일을 이동할 수 있습니다. 이것이 Winmerge와 같은 도구가하는 일입니다. 앱에서 "복사"버튼을 클릭하면 Explorer를 사용하여 사본을 정렬 한 것처럼 Windows 진행 상자가 나타납니다. 이 스레드 설명합니다.

최근 VB .NET에서 FileStreams를 사용하여 파일 사본을 구현했습니다.

fsSource = New FileStream(backupPath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None, 1024, FileOptions.WriteThrough)
fsDest = New FileStream(restorationPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 1024, FileOptions.WriteThrough)
TransferData(fsSource, fsDest, 1048576)

    Private Sub TransferData(ByVal FromStream As IO.Stream, ByVal ToStream As IO.Stream, ByVal BufferSize As Integer)
        Dim buffer(BufferSize - 1) As Byte

        Do While IsCancelled = False 'Do While True
            Dim bytesRead As Integer = FromStream.Read(buffer, 0, buffer.Length)
            If bytesRead = 0 Then Exit Do
            ToStream.Write(buffer, 0, bytesRead)
            sizeCopied += bytesRead
        Loop
    End Sub

ProgressBar (SizeCopied 포함)를 업데이트하고 필요한 경우 파일 전송을 취소하는 매우 쉬운 방법입니다 (ISCancelled).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top