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; } } }
progressBar中的Minimum设置为0。
然后做个事件,
每拷贝N大小的文件,就给
progressBar中的value属性赋值。一般赋已经拷贝过来的文件大小。
就这样,每激活一次事件,value就有个新值,于是每次激活事件,progressBar都会变长。
获得大小要看你是从哪拷贝。ftp,http,本机
拷贝的大小即value值可以在过程中取得。
比如要触发100次就
if(已读取大小>总大小/100)
{
ProcessUpdateEventArgs Args = new ProcessUpdateEventArgs(//传递参数,比如已读取/拷贝的文件大小);
ProcessUpdated(this, Args);//触发事件。
}
还有一点,马上给分:)
if(已读取大小>总大小/100) //已读取大小在甚么地方获取,此时Copy函数已经开始执行了啊!?
{
ProcessUpdateEventArgs Args = new ProcessUpdateEventArgs(//传递参数,比如已读取/拷贝的文件大小);
ProcessUpdated(this, Args);//触发事件。
}
if(已读取大小>总大小/100)
{
ProcessUpdateEventArgs Args = new ProcessUpdateEventArgs(//传递参数,比如已读取/拷贝的文件大小);
ProcessUpdated(this, Args);//触发事件。
}这样肯定不行!
有人指点吗?谢谢大家了!
因为两个工作是并进的,所以需要在循环中增加进度条
一种方法
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()
而不能用另外的类
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();
}
}
}
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;
}
}
}