C# winform 文件批量拷贝到目标文件夹
下面代码实现了从源文件夹拷贝所有文件到目标文件夹
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _1124_test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog dilog = new FolderBrowserDialog();
if (dilog.ShowDialog() == DialogResult.OK || dilog.ShowDialog() == DialogResult.Yes)
{
textBox1.Text = dilog.SelectedPath;
}
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog dilog = new FolderBrowserDialog();
dilog.ShowDialog();
textBox2.Text = dilog.SelectedPath;
}
private void button3_Click(object sender, EventArgs e)
{
DirectoryInfo thefolder = new DirectoryInfo(textBox1.Text);
foreach (FileInfo nextfile in thefolder.GetFiles())
{
try
{
string filename = nextfile.Name;
string filefullname = nextfile.FullName;
string mudi = textBox2.Text + "\\" + filename;
//如果目的文件已经存在
if (File.Exists(mudi))
{
File.Delete(mudi);
}
else
{
File.Copy(filefullname, mudi);
MessageBox.Show("已将文件复制到目标文件夹!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
}