本文介绍了我必须包含进度条以显示压缩文件的百分比。我写了代码,但进度条不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FileInfo fileToCompress = fileInfo;
                    using (FileStream originalFileStream = fileToCompress.OpenRead())
                    {
                        if ((File.GetAttributes(fileToCompress.FullName) &
                           FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != GZipConstants.GZExtension)
                        {
                           int length=Convert.ToInt32(originalFileStream.Length);
                            //string gzFileName = fileToCompress.FullName + GZipConstants.GZExtension;
                            string gzFileName;
                            if (!String.IsNullOrEmpty(targetDir))
                            {
                                string fileDetails = fileToCompress.FullName;
                                string getFileName = Path.GetFileName(fileDetails);
                                gzFileName = targetDir + getFileName + GZipConstants.GZExtension;
                                disp = getFileName;
                                //System.IO.Directory.CreateDirectory(targetDir + getFileName); // user provided path
                            }
                            else
                            {
                                string fileDetails = fileToCompress.FullName;
                                string getFileName = Path.GetFileName(fileDetails);
                                disp = getFileName;
                                gzFileName = fileToCompress.FullName + GZipConstants.GZExtension;
                            }
                            if (File.Exists(gzFileName))
                            {
                                File.Delete(gzFileName);
                            }
                            //int nPercentToAdvance = (100 / filesTPF.Length);
                            // create .gz file and copy the data
                            using (FileStream compressedFileStream = new FileStream(gzFileName, FileMode.Create, FileAccess.Write))
                            {
                                using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                                {
                                    // originalFileStream.CopyTo(compressionStream); // read all the bytes from the current stream  and writes them to destination stream

                                    // Copy the source file into the compression stream.
                                    byte[] buffer = new byte[GZipConstants.GZBufferSize];
                                    int numRead;
                                    int countPrg = 0;
                                    while ((numRead = originalFileStream.Read(buffer, 0, buffer.Length)) != GZipConstants.ZERO)
                                    {
                                        if ((cancel!=null)&&cancel())
                                        {
                                            break;
                                        }
                                        compressionStream.Write(buffer, GZipConstants.ZERO, numRead);
                                        countPrg = numRead + countPrg;
                                        if (countPrg==length)
                                        {
                                            progressStatus((countPrg * 100) / length, GZipConstants.GZipFile + fileToCompress.Name);
                                        }

                                    }
                            
                                }
                            }

                        }
                    }

推荐答案

if (countPrg==length)
{
    Console.WriteLine("countPrg: {0} length: {1}", countPrg, length);

    progressStatus((countPrg * 100) / length, GZipConstants.GZipFile + fileToCompress.Name);
}

假设您的应用运行没有错误并实际执行某些操作,如果您运行此代码,然后检查Visual Studio中的输出窗口,您应该能够诊断出多少次已调用更新进度条的代码。我怀疑它只被调用一次。

Assuming your app runs without errors and actually does something, if you run this code, and then examine the 'Output window in Visual Studio, you should be able to diagnose how many times the code to update the progress bar has been called. I suspect it's being called only once.


这篇关于我必须包含进度条以显示压缩文件的百分比。我写了代码,但进度条不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 15:32