嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
Windows ROBOCOPY 命令的 UI,依赖 .net 2.0
using System;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
namespace ROBOCOPY_UI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//InitInfo();
}
/// <summary>
/// 构建命令行及其参数的字符串
/// </summary>
/// <returns>命令行及参数</returns>
private string GetArgument()
{
// 问题:重复执行,文本框不刷新,不能自动结束进程
StringBuilder CommandString = new StringBuilder();
CommandString.Append("\"" SourcePath.Text.Trim() "\" \"" DestinationPath.Text.Trim() "\"");
// 是否复制空文件夹
if (radioButton1.Checked == true)
{
CommandString.Append(" /S");
}
else
{ CommandString.Append(" /E"); }
// 复制前 n 层文件夹
if (checkBox6.Checked == true && numericUpDown1.Value >0) { CommandString.Append("/LEV:" numericUpDown1.Value); }
// 删除源中不再存在的目标文件/目录
if (checkBox5.Checked == true) { CommandString.Append(" /PURGE");}
// 移动文件和目录(复制后从源中删除)
if (checkBox13.Checked == true) { CommandString.Append(" /MOVE"); }
// 使用可重新启动模式;如果拒绝访问,请使用备份模式,参数为“/ZB”
if (checkBox1.Checked == true && checkBox2.Checked == true) { CommandString.Append(" /ZB"); }
else
{
// 在可重新启动模式下复制文件
if (checkBox1.Checked == true) { CommandString.Append(" /Z"); }
// 在备份模式下复制文件
if (checkBox2.Checked == true) { CommandString.Append(" /B"); }
}
// 复制时使用未缓冲的 I/O (推荐在复制大文件时使用)。
if (checkBox3.Checked == true) { CommandString.Append(" /J"); }
// 排除大于 n MB 的文件
if (radioButton13.Checked == true && numericUpDown2.Value > 0) { CommandString.Append(" /MAX:" numericUpDown2.Value * 1048576); }
// 排除小于 n MB 的文件
if (radioButton14.Checked == true && numericUpDown3.Value > 0) { CommandString.Append(" /MIN:" numericUpDown3.Value * 1048576); }
// 失败副本的重试次数
if(checkBox4.Checked == true && numericUpDown4.Value > 0) { CommandString.Append(" /R:" numericUpDown4.Value); }
//
if(checkBox7.Checked == true && numericUpDown5.Value > 0) { CommandString.Append(" /W:" numericUpDown5.Value); }
return CommandString.ToString();
}
/// <summary>
/// 开始执行命令
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void go_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.SourcePath.Text.Trim()) || string.IsNullOrEmpty(this.DestinationPath.Text.Trim()))
{
MessageBox.Show("源文件夹或目标文件夹不可为空!");
}
else
{
using (Process process = new System.Diagnostics.Process()) // 新建进程,保用 using 可以自动回收资源,且无需判断是否结束
{
this.txtOutPutInfo.Text = "\r\n";
process.StartInfo.FileName = "ROBOCOPY"; // 命令
process.StartInfo.Arguments = GetArgument(); // 命令的参数
process.StartInfo.UseShellExecute = false; // 禁用系统 Shell
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // 隐藏窗体
process.StartInfo.CreateNoWindow = true; // 显示窗体
process.StartInfo.RedirectStandardError = true; // 重定向标准错误
process.StartInfo.RedirectStandardInput = true; // 重定向标准输入
process.StartInfo.RedirectStandardOutput = true; // 重定向标准输出
process.Start(); // 启动进程
//toolStripStatusLabel1.Text = " 进程运行中";
toolStripStatusLabel4.Text = process.StartInfo.FileName " " process.StartInfo.Arguments;
process.BeginOutputReadLine(); // 异步读取命令行的输出
process.OutputDataReceived = new DataReceivedEventHandler(process_OutputDataReceived); // 获取异步订阅事件
}
}
}
// 定义事件的方法
private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (String.IsNullOrEmpty(e.Data) == false) // 不为空则
{ this.AppendText(e.Data "\r\n"); } // 调用 AppendText 方法为 textBox1 追加文本和换行
}
#region 多线程下控件访问
public delegate void AppendTextCallback(string text); // 定义委托
// 重载 AppendText 方法
public void AppendText(string text)
{
if (this.txtOutPutInfo.InvokeRequired) // 是否多线程
{
AppendTextCallback d = new AppendTextCallback(AppendText);
this.txtOutPutInfo.Invoke(d, text);
}
else
{
this.txtOutPutInfo.AppendText(text);
}
}
#endregion
/// <summary>
/// 结束进程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
//KillProcess();
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog SourceFolder = new FolderBrowserDialog();
SourceFolder.ShowDialog();
SourcePath.Text = SourceFolder.SelectedPath;
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog DestinationFolder = new FolderBrowserDialog();
DestinationFolder.ShowDialog();
DestinationPath.Text = DestinationFolder.SelectedPath;
}
}
}