如题:比如我文件复制的时候,显示复制进度。那么对控件ProgressBar我要怎么做?原理或者代码!谢谢!

解决方案 »

  1.   

    progressBar中的Maximum设置为文件总大小。
     progressBar中的Minimum设置为0。
    然后做个事件,
    每拷贝N大小的文件,就给
    progressBar中的value属性赋值。一般赋已经拷贝过来的文件大小。
    就这样,每激活一次事件,value就有个新值,于是每次激活事件,progressBar都会变长。
      

  2.   

    还有问题,怎么获得楼上所说的所拷文件的大小,比如File.Copy正在运行,那么我怎么去获得已经拷贝过去文件的大小呢?谢谢!
      

  3.   

    先获得大小。再开始copy。
     获得大小要看你是从哪拷贝。ftp,http,本机
      

  4.   

    先获得总大小。再开始copy。
    拷贝的大小即value值可以在过程中取得。
      

  5.   

    关键是在拷贝的过程中,我还需要适时的获得复制文件的大小,这一点我很迷茫,Copy函数正在执行,怎么获得已经复制文件的大小?等他完成了,也就没有必要查看复制大小了,就在这一点上,有点迷糊,望指点!
      

  6.   

    不用,你自己写个copy程序就方便点,读取多少了之后触发事件。
    比如要触发100次就
    if(已读取大小>总大小/100)
    {
     ProcessUpdateEventArgs Args = new ProcessUpdateEventArgs(//传递参数,比如已读取/拷贝的文件大小);
     ProcessUpdated(this, Args);//触发事件。
    }
      

  7.   

    luffy927() 谢谢!
    还有一点,马上给分:)
    if(已读取大小>总大小/100)  //已读取大小在甚么地方获取,此时Copy函数已经开始执行了啊!?
    {
     ProcessUpdateEventArgs Args = new ProcessUpdateEventArgs(//传递参数,比如已读取/拷贝的文件大小);
     ProcessUpdated(this, Args);//触发事件。
    }
      

  8.   

    已读取大小 他是一个变量,也就是说Copy函数此时正在执行,if(已读取大小>总大小/100)函数放在甚么地方?File.COPy(strq,str2);
    if(已读取大小>总大小/100)  
    {
     ProcessUpdateEventArgs Args = new ProcessUpdateEventArgs(//传递参数,比如已读取/拷贝的文件大小);
     ProcessUpdated(this, Args);//触发事件。
    }这样肯定不行!
      

  9.   

    luffy927() 还在吗?
    有人指点吗?谢谢大家了!
      

  10.   

    所以说自己写个copy函数么,呵呵。这样,每次把文件读出来一些,读多少自己就知道了, value值就知道了。
      

  11.   

    还有个法子就是调用A,P,I。得在网上搜了。
      

  12.   

    backgroundWorker的应用
    因为两个工作是并进的,所以需要在循环中增加进度条
    一种方法
    this.backgroundWorker1.RunWorkerAsync();
    启动到doworkdowork
    做事情()progresschange 状态变化
    做事情()
    bw.ReportProgress(j);触发progresschange progresschange方法中
    this.progressBar1.PerformStep();这种方法就是用backgroundWorker来调用PerformStepbackgroundWorker的作用就是窗体互调。和delegate相似。多的就是知道什么时候工作结束,RunWorkerCompleted{this.Close();}还有可以中断CancelAsync 
    还一种方法
    直接调用prossece.PerformStep()
    这样就必须在form中声明
    prossece=new prossece()
    而不能用另外的类
      

  13.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form3 : Form
        {
            int sum=0;
            string strin;
            string strout;        public Form3()
            {
                InitializeComponent();
            }
            public Form3(int sum,string strin,string strout)
            {
                InitializeComponent();
                this.sum = sum;
                this.strin = strin;
                this.strout = strout;
            }
            private void Form3_Load(object sender, EventArgs e)
            {
                this.progressBar1.Maximum = sum;
                this.progressBar1.Step = 1;
                this.backgroundWorker1.RunWorkerAsync();
            }
            
            //backgroundWorker为了重复调用进度条 也是窗体间传参
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {            BackgroundWorker send = (BackgroundWorker)sender;
                Form2 f22 = new Form2();
                f22.copyDir(strin,strout,send,e);
                
                
            }
                  private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                this.progressBar1.PerformStep();
            }        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                this.Close();                                                                                                                                         this.DialogResult = DialogResult.OK;
            }        private void button2_Click(object sender, EventArgs e)
            {
                this.backgroundWorker1.CancelAsync();
            }
        }
    }
      

  14.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    namespace WindowsApplication1
    {
        public partial class Form2 : Form
        {
            static int sum;
            int sum1 = 0;
            string strin;
            string strout;        public Form2()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                strin = this.textBox1.Text;
                strout = this.textBox2.Text;            if (!strin.Substring(strin.Length - 1).Equals("\\"))
                {
                    strin = strin + "\\";
                }
                           if (!strout.Substring(strout.Length - 1).Equals("\\"))
                {
                    strout = strout + "\\";
                }
                
                jisuan(strin);
                
                
                this.label1.Text = sum.ToString();
                Form3 f3 = new Form3(sum,strin,strout);             DialogResult dia = f3.ShowDialog();
                 if (dia == DialogResult.OK) {
                     MessageBox.Show("ok");
                     System.Diagnostics.Process.Start(strout);
                 }                   }
            private void jisuan(string strin)
            {
                DirectoryInfo d = new DirectoryInfo(strin);
                FileSystemInfo[] f = d.GetFileSystemInfos();
                foreach (FileSystemInfo i in f)
                {
                    if (i is DirectoryInfo)
                    {                                       jisuan( strin + i.Name + "\\");
                    }
                    else if (i is FileInfo)
                    {                    sum++;                }
                }
            }        public  void copyDir(string strin,string strout,BackgroundWorker bw,DoWorkEventArgs e)
            {            
                DirectoryInfo d = new DirectoryInfo(strin);
                FileSystemInfo[] f = d.GetFileSystemInfos();
                foreach (FileSystemInfo i in f)
                {
                    if (i is DirectoryInfo)
                    {
                        DirectoryInfo di = new DirectoryInfo(strout+i.Name);
                        di.Create();
                                            copyDir(strin + i.Name + "\\", strout + i.Name + "\\",bw,e);
                    }
                    else if (i is FileInfo)
                    {
                        ((FileInfo)i).CopyTo(strout + i.Name, true);
                        sum1++;
                        int j = 100 * sum1 / sum;
                        bw.ReportProgress(j);
                    }
                }        }        private void button3_Click(object sender, EventArgs e)
            {
                this.folderBrowserDialog1.SelectedPath = "c://";
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.textBox1.Text = folderBrowserDialog1.SelectedPath;            }
            }        private void button4_Click(object sender, EventArgs e)
            {            this.folderBrowserDialog1.SelectedPath = "d://";
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.textBox2.Text = folderBrowserDialog1.SelectedPath;            }
            }        private void Form2_Load(object sender, EventArgs e)
            {
                 strin = this.textBox1.Text;
                 strout = this.textBox2.Text;
            }
        }
    }