c# - 文件复制进度条不更新。文件复制工作正常

标签 c# windows winforms

我正在编写一个用于复制文件的应用程序。我的文件复制没有任何问题,但进度条由于某种原因没有更新。我正在使用后台 worker 。这是代码:

private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // Gets the size of the file in bytes.
            Int64 iSize = strInputFile.Length;

            // Keeps track of the total bytes downloaded so we can update the progress bar.
            Int64 iRunningByteTotal = 0;
                // Open the input file for reading.
            using (FileStream InputFile = new FileStream(strInputFile, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                // Using the FileStream object, we can write the output file.
                using (FileStream OutputFile = new FileStream(strOutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    // Loop the stream and get the file into the byte buffer.
                    int iByteSize = 0;
                    byte[] byteBuffer = new byte[iSize];
                    while ((iByteSize = InputFile.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                    {
                        // Write the bytes to the file system at the file path specified.
                        OutputFile.Write(byteBuffer, 0, iByteSize);
                        iRunningByteTotal += iByteSize;

                        // Calculate the progress out of a base "100."
                        double dIndex = (double)(iRunningByteTotal);
                        double dTotal = (double)byteBuffer.Length;
                        double dProgressPercentage = (dIndex / dTotal);
                        int iProgressPercentage = (int)(dProgressPercentage * 100);
                        // Update the progress bar.
                       bgWorker.WorkerReportsProgress = true;
                        bgWorker.ReportProgress(iProgressPercentage);
                    }
                    // Close the output file.
                    OutputFile.Close();
                }
                // Close the input file.
                InputFile.Close();
            }
        }

        private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // We will increase the progress bar when work progress is reported.
            pbCopyProgress.Value = e.ProgressPercentage;
            pbCopyProgress.Text = (e.ProgressPercentage.ToString() + " %");
        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // Disable the Copy button once the file has been copied.
            MessageBox.Show("The file: "+strOutputFile+" has been copied");
            btnCopy.Enabled = false;
        }

最佳答案

我发现我需要初始化后台工作程序事件处理程序才能解决我遇到的问题。我只需要在表单加载事件中添加以下三行:

 bgWorker.DoWork += bgWorker_DoWork;
 bgWorker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
 bgWorker.ProgressChanged += bgWorker_ProgressChanged;

关于c# - 文件复制进度条不更新。文件复制工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20184848/

相关文章:

c# - 显示格式字符串,整数到百分比

java - FileOutputStream try-with-resources 不关闭文件描述符

windows - Netsh 设置规则取决于操作系统语言

c# - 为什么 Winforms 中有所有不同的集合?

c# - 如何强制执行某个 UserControl 设计

c# - StatusStrip 能否根据其项目的大小自动更改其高度?

c# - 使用 ASP.NET MVC 中的按钮处理页面操作的正确方法?

c# - 如何对 Windows API 常量进行分组

c# - NHibernate 3 和延迟加载的 future

windows - Carbon Accessibility API - 跨空间获取窗口信息