C# winForm 定时拷贝覆盖文件小工具
2021-06-24 02:06
标签:catch char orm 文件名 lca create split activate erb 代码大多来源于网络 IDE:vs2017 项目文件: 链接:https://pan.baidu.com/s/1DBUwVw3Blv2vsIBhEmTq6Q 界面: 代码: C# winForm 定时拷贝覆盖文件小工具 标签:catch char orm 文件名 lca create split activate erb 原文地址:https://www.cnblogs.com/nb08611033/p/10175383.html
提取码:qmok using System;
using System.Net;
using System.Windows.Forms;
using System.IO;
namespace CopyAndCoverFiles
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//窗体载入时执行
private void Form1_Load(object sender, EventArgs e)
{
//显示当前时间
Text = "北京时间(本地):" + DateTime.Now.ToString("yyyy年MM月dd dddd HH时mm分");
//启动显示时间的计时器
TimerShowTime.Start();
TimerShowTime.Interval = 60000;
//设置倒计时定时器间隔
TimerLastTime.Interval = 1000;//间隔1秒
}
//标题栏循环显示时间
private void TimerShowTime_Tick(object sender, EventArgs e)
{
ShowTime();
}
//标题栏显示时间
private void ShowTime()
{
if (GetNetDateTime() != "")
{
Text = "北京时间(网络):" + Convert.ToDateTime(GetNetDateTime()).ToString("yyyy年MM月dd dddd HH时mm分");
}
else
{
Text = "北京时间(本地):" + DateTime.Now.ToString("yyyy年MM月dd dddd HH时mm分");
}
}
//获取网络时间
private static string GetNetDateTime()
{
WebRequest request = null;
WebResponse response = null;
WebHeaderCollection headerCollection = null;
string datetime = string.Empty;
try
{
request = WebRequest.Create("https://www.baidu.com");
request.Timeout = 3000;
request.Credentials = CredentialCache.DefaultCredentials;
response = request.GetResponse();
headerCollection = response.Headers;
foreach (var h in headerCollection.AllKeys)
{
if (h == "Date")
{
datetime = headerCollection[h];
}
}
return datetime;
}
catch (Exception) { return datetime; }
finally
{
if (request != null)
{ request.Abort(); }
if (response != null)
{ response.Close(); }
if (headerCollection != null)
{ headerCollection.Clear(); }
}
}
//选择目标文件夹按钮
private void BtnSeleteTargetFolder_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
TxbTargetFolder.Text = folderBrowserDialog1.SelectedPath;
}
}
//选择源文件夹按钮
private void BtnSeleteSourceFolder_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
TxbSourceFolder.Text = folderBrowserDialog1.SelectedPath;
}
}
//输入源文件夹后遍历显示文件
private void TxbSourceFolder_TextChanged(object sender, EventArgs e)
{
if (TxbSourceFolder.Text.Trim() != "")
{
//初始化文件列表
CheckedListBoxFiles.Items.Clear();
//如果文件夹存在
if (Directory.Exists(TxbSourceFolder.Text.Trim()))
{
try
{
DirectoryInfo sourceFolder = new DirectoryInfo(TxbSourceFolder.Text);
//遍历显示文件
foreach (FileInfo nextFile in sourceFolder.GetFiles())
{
CheckedListBoxFiles.Items.Add(nextFile.Name);
}
}
catch (Exception ex)
{
MessageBox.Show("获取源文件内文件列表失败:" + ex.Message);
}
}
}
}
//执行按钮
private void BtnCountdown_Click(object sender, EventArgs e)
{
if (Checking())
{
//弹出警告窗口
DialogResult warning = MessageBox.Show("目标文件夹内的同名文件将被覆盖,执行前请备份好目标文件夹,继续吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (warning == DialogResult.OK)
{
Start();//开始执行
}
}
}
//暂停按钮
private void BtnPause_Click(object sender, EventArgs e)
{
Pause();//暂停执行
//修改设置的同步次数
if (toolStripStatusLabel3.Text != "无限")
{
NumTxbLoopCount.Value = Convert.ToDecimal(toolStripStatusLabel3.Text);
}
else
{
NumTxbLoopCount.Value = 0;
}
}
//倒数定时器
private void TimerCountdown_Tick(object sender, EventArgs e)
{
//暂停倒计时计时器
TimerLastTime.Stop();
//更新显示倒数次数
if (toolStripStatusLabel3.Text != "无限")
{
int loopCount = Convert.ToInt32(toolStripStatusLabel3.Text);
if (loopCount > 0)
{
loopCount--;
//启动倒计时计时器
TimerLastTime.Start();
if (loopCount == 0)
{
Pause();
}
}
toolStripStatusLabel3.Text = loopCount + "";
}
else
{
//启动倒计时计时器
TimerLastTime.Start();
}
//初始化倒计时显示
toolStripStatusLabel4.Text = SecondToTime(TimerCountdown.Interval / 1000);
//复制且覆盖文件
CopyAndCoverFile();
}
//复制且覆盖文件
private void CopyAndCoverFile()
{
if (Checking())
{
string targetFolderPath = TxbTargetFolder.Text + @"\";//目标文件夹路径
string sourceFolderPath = TxbSourceFolder.Text + @"\";//源文件夹路径
//遍历复制选中的文件
for (int i = 0; i )
{
string fileName = CheckedListBoxFiles.GetItemText(CheckedListBoxFiles.CheckedItems[i]);//取得文件名
try
{
File.Copy(sourceFolderPath + fileName, targetFolderPath + fileName, true);//复制覆盖同名文件
}
catch (Exception ex)
{
Pause();//暂停执行
MessageBox.Show("执行失败:" + ex.Message);
}
}
}
}
//倒计时定时器调用
private void TimerLastTime_Tick(object sender, EventArgs e)
{
//将时:分:秒转换为秒
string[] str = toolStripStatusLabel4.Text.Split(new char[] { ‘:‘ });
int lastTime = Convert.ToInt32(str[0]) * 3600 + Convert.ToInt32(str[1]) * 60 + Convert.ToInt32(str[2]);
//更新倒计时
if (lastTime == 1)
{
lastTime = TimerCountdown.Interval / 1000;
}
else
{
lastTime--;
}
toolStripStatusLabel4.Text = SecondToTime(lastTime);
}
//复制文件前检查
private bool Checking()
{
bool ok = false;
if (TxbTargetFolder.Text.Trim() != TxbSourceFolder.Text.Trim())
{
//如果目标文件夹存在
if (Directory.Exists(TxbTargetFolder.Text))
{
//如果勾选的文件数大于0
if (CheckedListBoxFiles.CheckedItems.Count > 0)
{
if (NumTxbHour.Value == 0 && NumTxbMinute.Value == 0 && NumTxbSecond.Value == 0)
{
MessageBox.Show("倒计时不能为0");
}
else
{
ok = true;
}
}
else
{
MessageBox.Show("请至少勾选一个文件");
}
}
//目标文件夹不存在
else
{
Pause();//暂停执行
MessageBox.Show("目标文件夹不存在,请重新选择");
}
}
else
{
MessageBox.Show("目标文件夹和源文件夹路径不能相同");
}
return ok;
}
//开始执行
private void Start()
{
GroupBoxForm.Enabled = false;//禁用界面输入
BtnCountdown.Enabled = false;//禁用执行按钮
BtnPause.Enabled = true;//启用暂停按钮
//执行间隔时间
int countdown = 0;
countdown += Convert.ToInt32(NumTxbHour.Value) * 3600;//小时
countdown += Convert.ToInt32(NumTxbMinute.Value) * 60;//分钟
countdown += Convert.ToInt32(NumTxbSecond.Value);//秒钟
//初始化倒计时显示
toolStripStatusLabel4.Text = SecondToTime(countdown);
//设置倒数计时器间隔
TimerCountdown.Interval = countdown * 1000;//单位毫秒
//启动倒数计时器
TimerCountdown.Start();
//启动倒计时定时器
TimerLastTime.Start();
//初始化剩余执行次数
if (NumTxbLoopCount.Value == 0)
{
toolStripStatusLabel3.Text = "无限";
}
else
{
toolStripStatusLabel3.Text = NumTxbLoopCount.Value + "";
}
}
//暂停执行
private void Pause()
{
//停止倒数定时器
TimerCountdown.Stop();
//停止倒计时定时器
TimerLastTime.Stop();
GroupBoxForm.Enabled = true;//启用界面输入
BtnCountdown.Enabled = true;//启用执行按钮
BtnPause.Enabled = false;//禁用暂停按钮
}
//将传入的秒钟转换为 时:分:秒 的字符串
private string SecondToTime(int countdown)
{
string time = "";
string[] arrLastTime = { "0", "0", "0" };//时,分,秒
arrLastTime[0] = countdown / 3600 + "";//得出小时
//得出分钟
if ((countdown % 3600) > 0)
{
arrLastTime[1] = (countdown % 3600) / 60 + "";
int second = (countdown % 3600) % 60;
if (second > 0)
{
arrLastTime[2] = second + "";
}
}
time = String.Join(":", arrLastTime);
return time;
}
//托盘图标双击
private void NotifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
//还原窗体显示
WindowState = FormWindowState.Normal;
//激活窗体并给予它焦点
Activate();
//任务栏区显示图标
ShowInTaskbar = true;
//托盘区图标隐藏
NotifyIcon1.Visible = false;
}
}
//窗口最小化时
private void Form1_SizeChanged(object sender, EventArgs e)
{
//判断是否选择的是最小化按钮
if (WindowState == FormWindowState.Minimized)
{
//隐藏任务栏区图标
ShowInTaskbar = false;
//图标显示在托盘区
NotifyIcon1.Visible = true;
}
}
}
}
上一篇:2.C#2.0之泛型
文章标题:C# winForm 定时拷贝覆盖文件小工具
文章链接:http://soscw.com/index.php/essay/97925.html