前几天硬盘坏了,资料都没有了,而且还没有备份,郁闷!写了个文件复制备份的程序,主要是将每天的程序备份到其他盘里面做好备份。直接文件拷贝时窗口不能移动,只好用线程来控制在后台复制,窗口显示文件大小和进度。windows复制拷贝文件都有时间预算的,这个不知道怎么实现了,呵呵,有知道的不要忘记告诉我一下,先谢了!
第一次写文章,有点晕晕的,希望大家看到的不是不知所云!
程序代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace FileCopy
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private System.Windows.Forms.Button ok;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBoxBackScroue;
private System.Windows.Forms.TextBox textBoxBackTrigger;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
ArrayList alFile = new ArrayList();
ArrayList alDir = new ArrayList();
private System.Windows.Forms.Label labelFile;
private System.Windows.Forms.Label labelDir;
private System.Windows.Forms.ProgressBar progressBar1;
int intParm = 0,intCopy =0;
long longFileSize = 0, longFileCopyindSize = 0;
private System.Windows.Forms.Label labelFileCopying;
private System.Windows.Forms.Label labelFileSize;
public Form1()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (components != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.ok = new System.Windows.Forms.Button();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.textBoxBackScroue = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.textBoxBackTrigger = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.labelFile = new System.Windows.Forms.Label();
this.labelDir = new System.Windows.Forms.Label();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.labelFileSize = new System.Windows.Forms.Label();
this.labelFileCopying = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// ok
//
this.ok.Location = new System.Drawing.Point(96, 96);
this.ok.Name = "ok";
this.ok.TabIndex = 0;
this.ok.Text = "拷贝";
this.ok.Click += new System.EventHandler(this.ok_Click);
//
// textBoxBackScroue
//
this.textBoxBackScroue.Location = new System.Drawing.Point(56, 8);
this.textBoxBackScroue.Name = "textBoxBackScroue";
this.textBoxBackScroue.ReadOnly = true;
this.textBoxBackScroue.Size = new System.Drawing.Size(168, 21);
this.textBoxBackScroue.TabIndex = 1;
this.textBoxBackScroue.Text = "E:\\工作区\\物资\\永安物流";
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 32);
this.label1.TabIndex = 2;
this.label1.Text = "要备份目录:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(232, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 23);
this.button1.TabIndex = 3;
this.button1.Text = "选择";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 40);
this.label2.TabIndex = 4;
this.label2.Text = "备份位置:";
//
// textBoxBackTrigger
//
this.textBoxBackTrigger.Location = new System.Drawing.Point(56, 56);
this.textBoxBackTrigger.Name = "textBoxBackTrigger";
this.textBoxBackTrigger.ReadOnly = true;
this.textBoxBackTrigger.Size = new System.Drawing.Size(168, 21);
this.textBoxBackTrigger.TabIndex = 5;
this.textBoxBackTrigger.Text = "D:\\备份";
//
// button2
//
this.button2.Location = new System.Drawing.Point(232, 56);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(48, 23);
this.button2.TabIndex = 6;
this.button2.Text = "选择";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// labelFile
//
this.labelFile.Location = new System.Drawing.Point(8, 136);
this.labelFile.Name = "labelFile";
this.labelFile.Size = new System.Drawing.Size(272, 40);
this.labelFile.TabIndex = 7;
//
// labelDir
//
this.labelDir.Location = new System.Drawing.Point(8, 184);
this.labelDir.Name = "labelDir";
this.labelDir.Size = new System.Drawing.Size(272, 40);
this.labelDir.TabIndex = 8;
//
// progressBar1
//
this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.progressBar1.Location = new System.Drawing.Point(0, 262);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(288, 23);
this.progressBar1.TabIndex = 9;
//
// labelFileSize
//
this.labelFileSize.Location = new System.Drawing.Point(8, 232);
this.labelFileSize.Name = "labelFileSize";
this.labelFileSize.Size = new System.Drawing.Size(128, 23);
this.labelFileSize.TabIndex = 10;
//
// labelFileCopying
//
this.labelFileCopying.Location = new System.Drawing.Point(168, 232);
this.labelFileCopying.Name = "labelFileCopying";
this.labelFileCopying.Size = new System.Drawing.Size(104, 23);
this.labelFileCopying.TabIndex = 11;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(288, 285);
this.Controls.Add(this.labelFileCopying);
this.Controls.Add(this.labelFileSize);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.labelDir);
this.Controls.Add(this.labelFile);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBoxBackTrigger);
this.Controls.Add(this.label2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBoxBackScroue);
this.Controls.Add(this.label1);
this.Controls.Add(this.ok);
this.Name = "Form1";
this.Text = "文件拷贝";
this.ResumeLayout(false);
}
#endregion
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Run(new Form1());
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 拷贝文件
/// </summary>
/// <param name="stringFile"></param>
private void FileCopy(string stringFile,string stringDirTigger)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] strFiles;
string[] strDirs;
string strDirTigger =stringDirTigger;
string strDirName ="";
// 建立当然目录
if(Directory.Exists( stringFile))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strFiles = Directory.GetFiles(stringFile);
foreach(string strFile in strFiles)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strDirName = strFile.Substring(stringFile.Length +1);
strDirTigger = stringDirTigger + "\\" + strDirName ;
FileCopy(strFile,strDirTigger);
}
strDirs = Directory.GetDirectories(stringFile);
foreach(string strFile in strDirs)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strDirName = strFile.Substring(stringFile.Length +1);
strDirTigger = stringDirTigger + "\\" + strDirName ;
Directory.CreateDirectory(strDirTigger);
FileCopy(strFile, strDirTigger);
}
}
else if(File.Exists(stringFile))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
labelDir.Text = "从 " + alDir[intCopy].ToString() + " 到 "+ alDir[intCopy].ToString() ;
labelFile.Text = "正在复制 " + alFile[intCopy].ToString();
File.Copy(stringFile,stringDirTigger);
FileInfo fi = new FileInfo(stringFile);
longFileCopyindSize += fi.Length;
labelFileCopying.Text = "已经复制 " + GetFileSize(longFileCopyindSize);
//progressBar1.Step =1;
progressBar1.Value = ++intCopy;
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获得文件名称
/// </summary>
/// <param name="stringFile"></param>
/// <returns></returns>
private string GetFileName(string stringFile)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return stringFile.Substring(stringFile.LastIndexOf("\\") + 1);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获得目录名称
/// </summary>
/// <param name="stringFile"></param>
/// <returns></returns>
private string GetDirName(string stringFile)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string stringDir;
stringDir = stringFile.Substring(0,stringFile.LastIndexOf("\\"));
stringDir = stringDir.Substring(stringDir.LastIndexOf("\\") + 1);
return stringDir;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获得目录的大小及文件列表
/// </summary>
/// <param name="stringFile"></param>
/// <param name="stringDirTigger"></param>
private void FileCopyStat(string stringFile,string stringDirTigger)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] strFiles;
string[] strDirs;
string strDirTigger =stringDirTigger;
string strDirName ="";
string stringParmFile ="";
string stringParmDir = "";
// 建立当前目录
if(Directory.Exists( stringFile))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strFiles = Directory.GetFiles(stringFile);
foreach(string strFile in strFiles)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strDirName = strFile.Substring(stringFile.Length +1);
strDirTigger = stringDirTigger + "\\" + strDirName ;
FileCopyStat(strFile,strDirTigger);
}
strDirs = Directory.GetDirectories(stringFile);
foreach(string strFile in strDirs)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strDirName = strFile.Substring(stringFile.Length +1);
strDirTigger = stringDirTigger + "\\" + strDirName ;
FileCopyStat(strFile, strDirTigger);
}
}
else if(File.Exists(stringFile))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
stringParmFile = GetFileName(stringFile);
alFile.Add(stringParmFile);
stringParmDir = GetDirName(stringFile);
FileInfo fi = new FileInfo(stringFile);
longFileSize += fi.Length;
labelFileSize.Text = GetFileSize(longFileSize);
alDir.Add(stringParmDir);
intParm++;
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 转换文件大小
/// </summary>
/// <param name="longFileSize"></param>
/// <returns></returns>
private string GetFileSize(long longFileSize)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
long longK = 1024;
long longM = 1024*1024;
long longFileSizeReturn;
string stringFileSize="";
longFileSizeReturn = longFileSize / longM ;
if(longFileSizeReturn >= 1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
stringFileSize =longFileSizeReturn.ToString() + "M";
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
longFileSizeReturn = longFileSize / longK ;
stringFileSize =longFileSizeReturn.ToString() + "K";
}
return stringFileSize;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设置初始
/// </summary>
private void setInit()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
intCopy = 0;
intParm = 0;
progressBar1.Value =intCopy;
alDir.Clear();
alFile.Clear();
labelDir.Text = "";
labelFile.Text = "";
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 复制文件
/// </summary>
private void CopyFiles()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string stringDirMain;
string strDirTrigger;
string strDirTime = System.DateTime.Now.ToString("yyyyMMddHHmmss");
stringDirMain = textBoxBackScroue.Text;
stringDirMain = stringDirMain.Substring(stringDirMain.LastIndexOf("\\"));
strDirTrigger = textBoxBackTrigger.Text + "\\" + strDirTime;
Directory.CreateDirectory(strDirTrigger);
strDirTrigger += "\\" + stringDirMain;
// 创建备份时间目录
Directory.CreateDirectory(strDirTrigger);
// 读取文件大小设置
FileCopyStat(textBoxBackScroue.Text, strDirTrigger);
progressBar1.Maximum = intParm;
// 复制文件到目标目录
FileCopy(textBoxBackScroue.Text, strDirTrigger);
// 设置原始状态
setInit();
Application.Exit();
}
private void button1_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(DialogResult.OK==folderBrowserDialog1.ShowDialog())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
textBoxBackScroue.Text = folderBrowserDialog1.SelectedPath;
}
}
private void ok_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 以线程方式处理文件
System.Threading.Thread th =new System.Threading.Thread(new System.Threading.ThreadStart(CopyFiles));
th.Start();
}
private void button2_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(DialogResult.OK==folderBrowserDialog1.ShowDialog())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
textBoxBackTrigger.Text = folderBrowserDialog1.SelectedPath;
}
}
}
}