2

我正在编写一个(相当)简单的 C# 应用程序,使用 .NET 4 在运行可执行文件之前检查更新。如果网络共享上存在较新版本的 exe,只需将其复制到本地文件夹并启动它。一切都很完美,除了在阅读 File.Copy() 的限制时,我意识到在执行此操作时我将无法显示进度条,而且我看到的所有内容都说使用 CopyFileEx,所以我我试图这样做。

我使用了此处找到的示例代码,它编译得很好(尽管我仍然有点不确定后台工作程序究竟是如何发挥作用的),除非我实际运行应用程序时,CopyFilEx() 方法返回 false,与错误是“参数不正确”。

我的代码(仅相关部分,如果需要我会添加更多)

调用函数:

XCopy.Copy(strServerAppPath + strExeName, strLocalAppPath + strExeName, true, true, (o,    pce) =>
{
worker.ReportProgress(pce.ProgressPercentage, strServerAppPath + strExeName);
});

(源路径计算为“C:\test.txt”,目标路径计算为“C:\test\test.txt”)

上面链接的代码中出现错误的地方:

bool result = CopyFileEx(Source, Destination, new CopyProgressRoutine(CopyProgressHandler), IntPtr.Zero, ref IsCancelled, copyFileFlags);
            if (!result)
                throw new Win32Exception(Marshal.GetLastWin32Error());

在此先感谢您的帮助,我已经为此苦苦挣扎了几个小时...

4

2 回答 2

3

与其处理所有的编组,不如简单地“滚动你自己的”复印机,逐块进行:

private static void CopyFile(string source, string destination, int bytesPerChunk)
{
    int bytesRead = 0;

    using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read))
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            using (FileStream fsDest = new FileStream(destination, FileMode.Create))
            {
                BinaryWriter bw = new BinaryWriter(fsDest);
                byte[] buffer;

                for (int i = 0; i < fs.Length; i += bytesPerChunk)
                {
                    buffer = br.ReadBytes(bytesPerChunk);
                    bw.Write(buffer);
                    bytesRead += bytesPerChunk;
                    ReportProgress(bytesRead, fs.Length);  //report the progress
                }
            }
        }
    }
}
于 2012-04-27T16:51:52.383 回答
3

与其在每次调用时ReadBytes()分配一个新的byte[]缓冲区数组的调用不同,不如分配一个缓冲区(例如大小为 64KB)并调用Read(buf, 0, buf.Length),它将最多读取buf.Length字节到数组中,然后返回实际读取的字节数。然后为每次读取重新使用相同的缓冲区数组(在将其内容写入目标流之后)。这样就不必为每个读/写操作重新分配一个新的缓冲区。

例子

例如,流复制方法的内部循环看起来像这样:

byte[]  buf;

// Allocate an I/O data buffer
buf = new byte[64*1024];

// Copy the contents of the input stream to the output stream
for (;;)
{
    int     len;

    // Read a block of data from the input stream
    len = inp.ReadBytes(buf, 0, buf.Length);
    if (len <= 0)
        break;

    // Write the data to the output stream
    outp.Write(buf, 0, len);
}

循环从输入流中读取多达 64KB 的字节到缓冲区,然后将读取的实际字节数写入输出流。每个读/写操作都使用相同的缓冲区,因此我们不会对缓冲区进行不必要的分配和释放。当读取操作失败时,我们已经到达输入流的末尾,所以我们退出循环。

于 2014-06-03T21:08:27.720 回答