添加进度条以指示文件已完成处理

时间:2012-08-31 21:09:16

标签: c# .net progress-bar

我是C#编程的新手,我很难在如何在项目文件中放置进度条。你能帮我解决一下吗?

以下是我的代码:

try
{
    lblUpdate.Visible = true;
    lblUpdate.Refresh();

    string[] filenames = Directory.GetFiles(sTargetFolderPath);

    // Zip up the files - From SharpZipLib Demo Code
    using (ZipOutputStream s = new ZipOutputStream(File.Create(lblSaveTo.Text + "\\" + sZipFileName + ".pld")))
    {
        s.SetLevel(9); // 0-9, 9 being the highest level of compression

        byte[] buffer = new byte[4096];

        foreach (string file in filenames)
        {

            ZipEntry entry = new ZipEntry(Path.GetFileName(file));

            entry.DateTime = DateTime.Now;
            s.PutNextEntry(entry);

            using (FileStream fs = File.OpenRead(file))
            {
                int sourceBytes;
                do
                {
                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                    s.Write(buffer, 0, sourceBytes);

                } while (sourceBytes > 0);
            }
        }
        s.Finish();
        s.Close();
    }
}

2 个答案:

答案 0 :(得分:0)

您必须使用后台工作程序才能在处理文件时冻结UI 在DoWork方法中设置你的过程 并使用Progress事件设置进度条 这里有一篇解释如何的文章 http://www.dotnetperls.com/progressbar 将您的进度条最大属性设置为filenames.Length

的文件数
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Shown += new EventHandler(Form1_Shown);

            // To report progress from the background worker we need to set this property
            backgroundWorker1.WorkerReportsProgress = true;
            // This event will be raised on the worker thread when the worker starts
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            // This event will be raised when we call ReportProgress
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        }
        void Form1_Shown(object sender, EventArgs e)
        {
            // Start the background worker
            backgroundWorker1.RunWorkerAsync();
        }
        // On worker thread so do our thing!
        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
           int i = 0; 
            foreach (string file in filenames)    
            {
                i++;
                // Report progress to 'UI' thread
                backgroundWorker1.ReportProgress(i);
               // Your background task goes here zip files
            }
        }
        // Back on the 'UI' thread so we can update the progress bar
        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // The progress percentage is a property of e
            progressBar1.Value = e.ProgressPercentage;
        }
    }

答案 1 :(得分:-1)

从工具书中创建一个进度条对象(只需拖放{或者您可以通过代码自己制作一个,无论您想要什么})

然后获取您运行的文件数量(100 / nNumOfFiles)并按此数量推进该条形图这里循环的每次迭代都是编译代码,其中'prbProgressBar'是进度条

    try
{

lblUpdate.Visible = true;
lblUpdate.Refresh();
string[] filenames = Directory.GetFiles(sTargetFolderPath);

// Zip up the files - From SharpZipLib Demo Code
using (ZipOutputStream s = new ZipOutputStream(File.Create(lblSaveTo.Text + "\\" + sZipFileName + ".pld")))
{
    s.SetLevel(9); // 0-9, 9 being the highest level of compression

    byte[] buffer = new byte[4096];
    int nPercentToAdvance = (100 / filenames.Length);
    foreach (string file in filenames)
    {

        ZipEntry entry = new ZipEntry(Path.GetFileName(file));

        entry.DateTime = DateTime.Now;
        s.PutNextEntry(entry);

        using (FileStream fs = File.OpenRead(file))
        {
            int sourceBytes;
            do
            {
                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                s.Write(buffer, 0, sourceBytes);

            } while (sourceBytes > 0);
        }

        this.prbProgressBar.Value += nPercentToAdvance;
    }
    s.Finish();
    s.Close();
}

}