-4

我是 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();
    }
}
4

2 回答 2

0

您必须使用后台工作人员以便在处理 DoWork 方法中设置的文件时不冻结 UI 您的进程并使用 Progress 事件在此处设置 进度progressbar 文件数的最大属性,即文件名。长度

    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;
        }
    }
于 2012-08-31T22:48:09.640 回答
-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();
}

}

于 2012-08-31T22:21:30.480 回答