我必须包含进度条以显示压缩文件的百分比。我写了代码,但进度条不起作用。 [英] I have to include progress bar to show what percent of file is compressed. I have written the code but progress bar is not working.

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

问题描述

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);
                                        }

                                    }
                            
                                }
                            }

                        }
                    }

推荐答案

如果运行代码时出现任何错误:它们在代码中出现在哪里?



您是否尝试使用F11设置断点并单步执行代码并检查变量的变化值?



如果没有错误:



修改你(显然)更新进度条的地方,如下所示:
If there are any errors running your code: where do they occur in the code ?

Have you tried setting a break-point and single-stepping through your code with F11 and examining the changing values of variables ?

If there are no errors:

Modify the place where you (apparently) update the progress bar like this:
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.


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

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