下面是我得代码,文件总共大小有40M,1W个文件。我Copy到目标路径差不多要10来分钟,求大神帮解决下,如果加个比较的简单进度条来显示进度要怎么写?
private void button9_Click(object sender, EventArgs e)
{
if (this.textBox1.Text == "")
{
MessageBox.Show("Path is empty! ");
return;
}
try
{
for (int j = 0; j < this.listBox2.Items.Count; j++)
{
for (int i = 0; i < this.listBox3.Items.Count; i++)
{
if (bool.Parse((this.listBox2.Items[j].ToString().Equals(this.listBox3.Items[i].ToString())).ToString()))
{
// this.listBox2.Items.Remove(this.listBox3.Items[j].ToString());
string a = this.listBox2.Items[j].ToString();
string b = "B_F_SENSUS_" + a.Substring(0, 4) + "_" + a.Substring(4, 2) + "." + a.Substring(6, 2) + "." + a.Substring(8, 2)
+ "." + a.Substring(10, 2) + "." + a.Substring(12, 2) + "." + a.Substring(14, 2);
CopyDirectory(this.label1.Text, this.textBox1.Text, b);
} }
} MessageBox.Show("OK", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void CopyDirectory(string srcdir, string desdir,string name)
{ try
{
string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1); string desfolderdir = desdir + "\\" + folderName; if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
{
desfolderdir = desdir + folderName;
}
string[] filenames = Directory.GetFileSystemEntries(srcdir); foreach (string file in filenames)// 遍历所有的文件和目录
{
if (file.Equals(this.label1.Text + '\\' + name+".dat"))//路径全名称
{
string srcfileName = file.Substring(file.LastIndexOf("\\") + 1); srcfileName = desfolderdir + "\\" + srcfileName;
if (!Directory.Exists(desfolderdir))
{
Directory.CreateDirectory(desfolderdir);
} File.Copy(file, srcfileName, true);
}
}
} catch (Exception ex)
{ MessageBox.Show(ex.ToString());
}
}
private void button9_Click(object sender, EventArgs e)
{
if (this.textBox1.Text == "")
{
MessageBox.Show("Path is empty! ");
return;
}
try
{
for (int j = 0; j < this.listBox2.Items.Count; j++)
{
for (int i = 0; i < this.listBox3.Items.Count; i++)
{
if (bool.Parse((this.listBox2.Items[j].ToString().Equals(this.listBox3.Items[i].ToString())).ToString()))
{
// this.listBox2.Items.Remove(this.listBox3.Items[j].ToString());
string a = this.listBox2.Items[j].ToString();
string b = "B_F_SENSUS_" + a.Substring(0, 4) + "_" + a.Substring(4, 2) + "." + a.Substring(6, 2) + "." + a.Substring(8, 2)
+ "." + a.Substring(10, 2) + "." + a.Substring(12, 2) + "." + a.Substring(14, 2);
CopyDirectory(this.label1.Text, this.textBox1.Text, b);
} }
} MessageBox.Show("OK", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void CopyDirectory(string srcdir, string desdir,string name)
{ try
{
string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1); string desfolderdir = desdir + "\\" + folderName; if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
{
desfolderdir = desdir + folderName;
}
string[] filenames = Directory.GetFileSystemEntries(srcdir); foreach (string file in filenames)// 遍历所有的文件和目录
{
if (file.Equals(this.label1.Text + '\\' + name+".dat"))//路径全名称
{
string srcfileName = file.Substring(file.LastIndexOf("\\") + 1); srcfileName = desfolderdir + "\\" + srcfileName;
if (!Directory.Exists(desfolderdir))
{
Directory.CreateDirectory(desfolderdir);
} File.Copy(file, srcfileName, true);
}
}
} catch (Exception ex)
{ MessageBox.Show(ex.ToString());
}
}
或者你用api函数 SHFileOperation调用windows自己的copy功能
拷贝文件用:FastCopy.exe
多线程COPY这个有什么参考代码资料吗?
每组各自写各自的工作日志,所有组都执行完了,依次检查日志情况,根据日志整理目的文件夹。最后的检查也可以不要。