实例介绍
【实例简介】
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;
}
}
}
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论