最近正在学习C#,所以有关Java的学习心得只能断断续续进行了,这两天做了个C#文件复制的小程序,其实在C#中,文件复制非常简单,最简单的方法使用File.Copy(源文件,目标文件)就可实现了,在程序中,我主要是实现了根据后缀名来选择要复制的文件,顺便实现了源文件的排序(按大小,后缀名,名称,创建日期),在文件复制过程中伴有进度条和列表框的细节显示,不足之处还请大家指点。
首次发表文章却在java区写了个C#程序,还望大家和管理员谅解,我确实是个Java爱好者。
图片为运行效果图:
以下为程序代码:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Threading;
namespace FileCopy
{
public class FileCopy : System.Windows.Forms.Form
{
成员变量#region 成员变量
private Thread _th = null;
private ArrayList _ary = new ArrayList();//用于记录源路径的所有文件名称
private Hashtable _ht = new Hashtable();
private System.Windows.Forms.TextBox txtSrcPath;
private System.Windows.Forms.Button btnSrcPath;
private System.Windows.Forms.TextBox txtTargetPath;
private System.Windows.Forms.CheckedListBox chkListSuffix;
private System.Windows.Forms.CheckedListBox chkListName;
private System.Windows.Forms.ProgressBar proCopy;
private System.Windows.Forms.Button btnDetails;
private System.Windows.Forms.Button btnBegin;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.ContextMenu mnuName;
private System.Windows.Forms.ListBox listDetails;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialogSrc;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialogTarget;
private System.Windows.Forms.Button btnTargetPath;
#endregion
构造方法#region 构造方法
public FileCopy()
{
InitializeComponent();
}
#endregion
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FileCopy));
this.txtSrcPath = new System.Windows.Forms.TextBox();
this.btnSrcPath = new System.Windows.Forms.Button();
this.txtTargetPath = new System.Windows.Forms.TextBox();
this.btnTargetPath = new System.Windows.Forms.Button();
this.chkListSuffix = new System.Windows.Forms.CheckedListBox();
this.chkListName = new System.Windows.Forms.CheckedListBox();
this.proCopy = new System.Windows.Forms.ProgressBar();
this.listDetails = new System.Windows.Forms.ListBox();
this.btnDetails = new System.Windows.Forms.Button();
this.btnBegin = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.mnuName = new System.Windows.Forms.ContextMenu();
this.folderBrowserDialogSrc = new System.Windows.Forms.FolderBrowserDialog();
this.folderBrowserDialogTarget = new System.Windows.Forms.FolderBrowserDialog();
this.SuspendLayout();
//
// txtSrcPath
//
this.txtSrcPath.Location = new System.Drawing.Point(8, 16);
this.txtSrcPath.Name = "txtSrcPath";
this.txtSrcPath.ReadOnly = true;
this.txtSrcPath.Size = new System.Drawing.Size(256, 21);
this.txtSrcPath.TabIndex = 0;
this.txtSrcPath.Text = "";
this.txtSrcPath.TextChanged += new System.EventHandler(this.txtSrcPath_TextChanged);
//
// btnSrcPath
//
this.btnSrcPath.Location = new System.Drawing.Point(272, 16);
this.btnSrcPath.Name = "btnSrcPath";
this.btnSrcPath.Size = new System.Drawing.Size(88, 24);
this.btnSrcPath.TabIndex = 1;
this.btnSrcPath.Text = "源文件路径";
this.btnSrcPath.Click += new System.EventHandler(this.btnSrcPath_Click);
//
// txtTargetPath
//
this.txtTargetPath.Location = new System.Drawing.Point(8, 48);
this.txtTargetPath.Name = "txtTargetPath";
this.txtTargetPath.ReadOnly = true;
this.txtTargetPath.Size = new System.Drawing.Size(256, 21);
this.txtTargetPath.TabIndex = 2;
this.txtTargetPath.Text = "";
this.txtTargetPath.TextChanged += new System.EventHandler(this.txtTargetPath_TextChanged);
//
// btnTargetPath
//
this.btnTargetPath.Location = new System.Drawing.Point(272, 48);
this.btnTargetPath.Name = "btnTargetPath";
this.btnTargetPath.Size = new System.Drawing.Size(88, 24);
this.btnTargetPath.TabIndex = 3;
this.btnTargetPath.Text = "目标文件路径";
this.btnTargetPath.Click += new System.EventHandler(this.btnTargetPath_Click);
//
// chkListSuffix
//
this.chkListSuffix.CheckOnClick = true;
this.chkListSuffix.Location = new System.Drawing.Point(8, 80);
this.chkListSuffix.Name = "chkListSuffix";
this.chkListSuffix.Size = new System.Drawing.Size(96, 132);
this.chkListSuffix.TabIndex = 4;
this.chkListSuffix.SelectedIndexChanged += new System.EventHandler(this.chkListSuffix_SelectedIndexChanged);
//
// chkListName
//
this.chkListName.CheckOnClick = true;
this.chkListName.HorizontalScrollbar = true;
this.chkListName.Location = new System.Drawing.Point(112, 80);
this.chkListName.MultiColumn = true;
this.chkListName.Name = "chkListName";
this.chkListName.Size = new System.Drawing.Size(248, 132);
this.chkListName.TabIndex = 5;
this.chkListName.SelectedIndexChanged += new System.EventHandler(this.chkListName_SelectedIndexChanged);
//
// proCopy
//
this.proCopy.Location = new System.Drawing.Point(8, 224);
this.proCopy.Name = "proCopy";
this.proCopy.Size = new System.Drawing.Size(288, 24);
this.proCopy.TabIndex = 6;
this.proCopy.Visible = false;
//
// listDetails
//
this.listDetails.ItemHeight = 12;
this.listDetails.Location = new System.Drawing.Point(8, 256);
this.listDetails.Name = "listDetails";
this.listDetails.Size = new System.Drawing.Size(352, 112);
this.listDetails.TabIndex = 8;
this.listDetails.Visible = false;
//
// btnDetails
//
this.btnDetails.Enabled = false;
this.btnDetails.Location = new System.Drawing.Point(304, 224);
this.btnDetails.Name = "btnDetails";
this.btnDetails.Size = new System.Drawing.Size(56, 24);
this.btnDetails.TabIndex = 9;
this.btnDetails.Text = "细节";
this.btnDetails.Click += new System.EventHandler(this.btnDetails_Click);
//
// btnBegin
//
this.btnBegin.Enabled = false;
this.btnBegin.Location = new System.Drawing.Point(272, 376);
this.btnBegin.Name = "btnBegin";
this.btnBegin.Size = new System.Drawing.Size(88, 24);
this.btnBegin.TabIndex = 10;
this.btnBegin.Text = "开始复制";
this.btnBegin.Click += new System.EventHandler(this.btnBegin_Click);
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(8, 376);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(56, 24);
this.btnClose.TabIndex = 11;
this.btnClose.Text = "关闭";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// FileCopy
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 408);
this.ControlBox = false;
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnBegin);
this.Controls.Add(this.btnDetails);
this.Controls.Add(this.listDetails);
this.Controls.Add(this.proCopy);
this.Controls.Add(this.chkListName);
this.Controls.Add(this.chkListSuffix);
this.Controls.Add(this.btnTargetPath);
this.Controls.Add(this.txtTargetPath);
this.Controls.Add(this.btnSrcPath);
this.Controls.Add(this.txtSrcPath);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "FileCopy";
this.Text = "文件复制";
this.Load += new System.EventHandler(this.FileCopy_Load);
this.ResumeLayout(false);
}
#endregion
程序入口#region 程序入口
static void Main()
{
Application.Run(new FileCopy());
}
#endregion
加载窗体事件处理#region 加载窗体事件处理
private void FileCopy_Load(object sender, System.EventArgs e)
{
//文件排序相关菜单
MenuItem itemSort = new MenuItem();
itemSort.Text = "文件排序";
MenuItem itemSize = new MenuItem();
itemSize.Text = "大小";
itemSize.Click += new EventHandler(itemSize_Click);
itemSort.MenuItems.Add(itemSize);
MenuItem itemDate = new MenuItem();
itemDate.Text = "日期";
itemDate.Click += new EventHandler(itemDate_Click);
itemSort.MenuItems.Add(itemDate);
MenuItem itemName = new MenuItem();
itemName.Text = "名称";
itemName.Click += new EventHandler(itemName_Click);
itemSort.MenuItems.Add(itemName);
MenuItem itemType = new MenuItem();
itemType.Text = "类型";
itemType.Click += new EventHandler(itemType_Click);
itemSort.MenuItems.Add(itemType);
//文件编辑相关菜单
MenuItem itemEdit = new MenuItem();
itemEdit.Text = "编辑";
MenuItem itemSelect = new MenuItem();
itemSelect.Text = "全选";
itemSelect.Click += new EventHandler(itemSelect_Click);
itemEdit.MenuItems.Add(itemSelect);
MenuItem itemCancel = new MenuItem();
itemCancel.Text = "取消选定";
itemCancel.Click += new EventHandler(itemCancel_Click);
itemEdit.MenuItems.Add(itemCancel);
this.mnuName.MenuItems.Add(itemSort);
this.mnuName.MenuItems.Add(itemEdit);
this.chkListName.ContextMenu = this.mnuName;
}
#endregion
源文件路径事件处理#region 源文件路径事件处理
private void btnSrcPath_Click(object sender, System.EventArgs e)
{
this.folderBrowserDialogSrc.ShowDialog();
try
{
this._ary.Clear();
this._ht.Clear();
this.chkListSuffix.Items.Clear();
this.chkListName.Items.Clear();
//输出所选路径
this.txtSrcPath.Text = this.folderBrowserDialogSrc.SelectedPath;
string[] files = Directory.GetFiles(this.folderBrowserDialogSrc.SelectedPath);
this._ary.Clear();//清楚
foreach(string dir in files)
{
if(!this.chkListSuffix.Items.Contains(Path.GetExtension(dir)))
{
this.chkListSuffix.Items.Add(Path.GetExtension(dir));
}
this.chkListName.Items.Add(Path.GetFileName(dir));
//将全部文件名放入全局集合
this._ary.Add(Path.GetFileName(dir));
//保存该文件的相关信息,以便进行文件排序
FileInfo info = new FileInfo(dir);
MyFile mf = new MyFile();
mf.Size = info.Length.ToString();
mf.Date = info.CreationTime.ToFileTime().ToString();
mf.Name = Path.GetFileName(dir);
mf.Type = Path.GetExtension(dir);
this._ht.Add(mf.Name,mf);
}
//将后缀名复选框列表全部选中
for(int i=0;i<this.chkListSuffix.Items.Count;i++)
{
this.chkListSuffix.SetItemChecked(i,true);
}
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
MessageBox.Show("请选择要复制文件路径","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
#endregion
目标文件路径事件处理#region 目标文件路径事件处理
private void btnTargetPath_Click(object sender, System.EventArgs e)
{
this.folderBrowserDialogTarget.ShowDialog();
this.txtTargetPath.Text = this.folderBrowserDialogTarget.SelectedPath;
}
#endregion
关闭窗体#region 关闭窗体
private void btnClose_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
#endregion
文件后缀名复选列表更改事件#region 文件后缀名复选列表更改事件
private void chkListSuffix_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.chkListName.Items.Clear();
string name = null;
foreach(string suffix in this.chkListSuffix.CheckedItems)
{
foreach(string temp in this._ary)
{
name = (string)temp;
int num = name.LastIndexOf('.');
if(suffix == name.Substring(num))
{
this.chkListName.Items.Add(name);
}
}
}
this.isBegin();
}
#endregion
细节按钮事件处理#region 细节按钮事件处理
private void btnDetails_Click(object sender, System.EventArgs e)
{
this.listDetails.Visible = true;
this.btnDetails.Enabled = false;
}
#endregion
复制按钮事件处理#region 复制按钮事件处理
private void btnBegin_Click(object sender, System.EventArgs e)
{
if(this.btnBegin.Text == "复制完成")
{
this.copyComplete();
this.btnClose.Enabled = true;
return;
}
this.proCopy.Visible = true;
this.btnClose.Enabled = false;
//启动文件复制线程
this._th = new Thread(new ThreadStart(MyCopy));
this._th.Start();
}
#endregion
文件复制线程方法#region 文件复制线程方法
private void MyCopy()
{
this.btnDetails.Enabled = true;
this.btnBegin.Enabled = false;
this.btnBegin.Text = "正在复制";
this.listDetails.Items.Add("开始复制");
this.proCopy.Maximum = this.chkListName.CheckedItems.Count;
int errorNum = 0;//用于记录复制失败的文件数
int successNum = 0;
bool flag = false;
for(int i=0;i<this.chkListName.CheckedItems.Count;i++)
{
if(flag)//如果点击终止,跳出循环
{
this.btnBegin.Enabled = true;
break;
}
string name = this.chkListName.CheckedItems[i].ToString();
try
{
this.listDetails.Items.Add("正在复制文件---"+name);
File.Copy(this.txtSrcPath.Text+"\\"+name,this.txtTargetPath.Text+"\\"+name);
this.proCopy.Increment(1);
successNum ++;
}
catch(IOException e1)
{
DialogResult result = MessageBox.Show("文件"+name+"拒绝访问\n目标路径中可能存在相同文件","复制错误",MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon.Warning);
switch(result)
{
case DialogResult.Abort: //终止,退出循环
flag = true;
break;
case DialogResult.Ignore://忽略,记录出错文件数量
this.proCopy.Increment(1);
errorNum ++;
break;
case DialogResult.Retry: //重试,记数器减1
i --;
break;
}
}
catch(Exception e2)
{
MessageBox.Show(e2.Message);
}
}
if(this.proCopy.Value != this.proCopy.Maximum)
{
this.proCopy.Value = this.proCopy.Maximum;
}
this.listDetails.Items.Add("文件复制完成");
this.listDetails.Items.Add(successNum.ToString()+"个文件复制成功,有"+errorNum.ToString()+"个文件被忽略");
MessageBox.Show("文件复制完成","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.btnBegin.Text = "复制完成";
this.btnBegin.Enabled = true;
this.btnClose.Enabled = true;
this._th.Abort();
}
#endregion
文件名称复选列表更改事件#region 文件名称复选列表更改事件
private void chkListName_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.isBegin();
}
#endregion
根据条件判断开始复制按钮是否可用#region 根据条件判断开始复制按钮是否可用
private void isBegin()
{
if(this.chkListName.CheckedItems.Count == 0 || this.txtSrcPath.Text == "" || this.txtTargetPath.Text == "")
this.btnBegin.Enabled = false;
else
this.btnBegin.Enabled = true;
}
#endregion
路径文本改变事件#region 路径文本改变事件
private void txtTargetPath_TextChanged(object sender, System.EventArgs e)
{
this.isBegin();
}
private void txtSrcPath_TextChanged(object sender, System.EventArgs e)
{
this.isBegin();
}
#endregion
控件初始化状态#region 控件初始化状态
private void copyComplete()
{
this.proCopy.Value = 0;
this.listDetails.Items.Clear();
this.proCopy.Visible = false;
this.listDetails.Visible = false;
this.btnDetails.Enabled = false;
this.btnBegin.Text = "开始复制";
}
#endregion
全选菜单事件处理#region 全选菜单事件处理
private void itemSelect_Click(object sender, EventArgs e)
{
for(int i=0;i<this.chkListName.Items.Count;i++)
{
this.chkListName.SetItemChecked(i,true);
}
this.isBegin();
}
#endregion
取消选择菜单事件处理#region 取消选择菜单事件处理
private void itemCancel_Click(object sender, EventArgs e)
{
for(int i=0;i<this.chkListName.Items.Count;i++)
{
this.chkListName.SetItemChecked(i,false);
}
this.isBegin();
}
#endregion
按文件大小排序#region 按文件大小排序
private void itemSize_Click(object sender, EventArgs e)
{
ArrayList arySize = new ArrayList();//用于记录所有文件大小
string size = null;
MyFile mf = null;
int length = 0;
//遍历所有文件名,得到所有文件的大小并放入集合
foreach(string name in this.chkListName.Items)
{
mf = (MyFile)this._ht[name];
size = mf.Size;
length = System.Int32.Parse(size);
arySize.Add(length);
}
arySize.Sort();
ArrayList aryName = new ArrayList();//用于记录排序后的文件名
int len = 0;
Hashtable temp = (Hashtable)this._ht.Clone();
for(int i=0;i<arySize.Count;i++)
{
length = (int)arySize[i];
System.Collections.IDictionaryEnumerator dir = temp.GetEnumerator();
while(dir.MoveNext())
{
mf = (MyFile)temp[dir.Key];
size = mf.Size;
len = System.Int32.Parse(size);
if(length == len)
{
aryName.Add(dir.Key);
temp.Remove(dir.Key);
break;
}
}
}
this.chkListName.Items.Clear();
foreach(string name in aryName)
{
this.chkListName.Items.Add(name);
}
}
#endregion
按文件日期排序#region 按文件日期排序
private void itemDate_Click(object sender, EventArgs e)
{
ArrayList arySize = new ArrayList();//用于记录所有文件创建时间的长整型格式
string size = null;
MyFile mf = null;
long length = 0;
//遍历所有文件名,得到所有文件的创建时间并放入集合
foreach(string name in this.chkListName.Items)
{
mf = (MyFile)this._ht[name];
size = mf.Date;
length = System.Int64.Parse(size);
arySize.Add(length);
}
arySize.Sort();
ArrayList aryName = new ArrayList();//用于记录排序后的文件名
long len = 0;
Hashtable temp = (Hashtable)this._ht.Clone();
for(int i=0;i<arySize.Count;i++)
{
length = (long)arySize[i];
System.Collections.IDictionaryEnumerator dir = temp.GetEnumerator();
while(dir.MoveNext())
{
mf = (MyFile)temp[dir.Key];
size = mf.Date;
len = System.Int64.Parse(size);
if(length == len)
{
aryName.Add(dir.Key);
temp.Remove(dir.Key);
break;
}
}
}
this.chkListName.Items.Clear();
foreach(string name in aryName)
{
this.chkListName.Items.Add(name);
}
}
#endregion
按文件名称排序#region 按文件名称排序
private void itemName_Click(object sender, EventArgs e)
{
ArrayList ary = new ArrayList();
foreach(string name in this.chkListName.Items)
{
ary.Add(name);
}
ary.Sort();
this.chkListName.Items.Clear();
foreach(string aa in ary)
{
this.chkListName.Items.Add(aa);
}
}
#endregion
按文件类型排序#region 按文件类型排序
private void itemType_Click(object sender, EventArgs e)
{
ArrayList arySize = new ArrayList();//用于记录所有文件的后缀名
string type = null;
MyFile mf = null;
//遍历所有文件名,得到所有文件的后缀名并放入集合
foreach(string name in this.chkListName.Items)
{
mf = (MyFile)this._ht[name];
type = mf.Type;
arySize.Add(type);
}
arySize.Sort();
ArrayList aryName = new ArrayList();//用于记录排序后的文件名
string len = null;
Hashtable temp = (Hashtable)this._ht.Clone();
for(int i=0;i<arySize.Count;i++)
{
len = (string)arySize[i];
System.Collections.IDictionaryEnumerator dir = temp.GetEnumerator();
while(dir.MoveNext())
{
mf = (MyFile)temp[dir.Key];
type = mf.Type;
if(type.Equals(len))
{
aryName.Add(dir.Key);
temp.Remove(dir.Key);
break;
}
}
}
this.chkListName.Items.Clear();
foreach(string name in aryName)
{
this.chkListName.Items.Add(name);
}
}
#endregion
}
}
using System;
namespace FileCopy
{
/**//// <summary>
/// 该类用于记录源文件的相关信息
/// </summary>
public class MyFile
{
private string _size;//大小
private string _date;//日期
private string _name;//名称
private string _type;//类型
public string Size
{
set
{
this._size = value;
}
get
{
return this._size;
}
}
public string Date
{
set
{
this._date = value;
}
get
{
return this._date;
}
}
public string Name
{
set
{
this._name = value;
}
get
{
return this._name;
}
}
public string Type
{
set
{
this._type = value;
}
get
{
return this._type;
}
}
}
}
点击这里下载源代码:/Files/daizhenghenry/Practise10.rar