using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics; namespace UpdateDllApplication1
{
public partial class Form1 : Form
{
// 思路:
// 先建立一个表A 保存 所有DLL 位置 和 DLL 修改时间
// 然后建立一个表B 保存 有最大修改时间的 DLL 位置 和 DLL 修改时间
// 此两个表建立完成后 从表B 每个DLL 依次在 表A 找修改时间小于表B 同名DLL 的
// 然后 复制 并写入到ListBox1! private bool Stop = false; private struct Filees
{
/// <summary>
/// 路径+文件名
/// </summary>
public string FullName;
/// <summary>
/// 文件名
/// </summary>
public string FileName;
/// <summary>
/// 修改时间
/// </summary>
public DateTime ModifyTime;
} public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fd = new FolderBrowserDialog();
fd.Description = "选择一个文件夹,此程序将更新此文件夹中所有DLL 至最新版本";
fd.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //我的文档
fd.ShowNewFolderButton = false;
if (fd.ShowDialog(this) == DialogResult.OK)
{
textBox1.Text = fd.SelectedPath;
}
} private void button2_Click(object sender, EventArgs e)
{
// 扫描文件夹中 DLL 位置 和 修改时间 并保存到两个表
// 一个表里保存的全部
// 另一表里保存的最新修改时间的 DLL 位置和时间 // 下面代码有误 只能查当前文件夹, 正确写法需要递归
List<Filees> A = new List<Filees>();
List<Filees> B = new List<Filees>();
// 切莫忘了清空 如果不需要即时清空 也要添加右键菜单的清空操作.
listBox1.Items.Clear();
string path = textBox1.Text.Trim();
if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
{
SearchFiles(path, A, B);
}
// 开始修改
bool checkVer = checkBox1.Checked;
if (MessageBox.Show(this, "这样会修改很多文件,开始前需要确认", "提示") == DialogResult.OK)
{
foreach (Filees item in A.ToArray())
{
if (Stop)
return;
foreach (Filees items in B.ToArray())
{
if (Stop)
return;
if (item.FileName == items.FileName)
{
if (!checkVer || (checkVer && ver(item.FullName) == ver(items.FullName))) //检查版本号
{
if (item.ModifyTime < items.ModifyTime)
{
listBox1.Items.Add(item.FullName);
try
{
File.Copy(items.FullName, item.FullName, true);
}
catch (Exception ex)
{
listBox1.Items.Add(" 出错!!!!!!\t" + ex.Message);
}
}
}
}
}
}
MessageBox.Show(this, "已完成!!!", "提示");
}
} private string ver(string path)
{
try
{
return System.Reflection.Assembly.LoadFrom(path).GetName().Version.ToString();
}
catch (Exception ex)
{
listBox1.Items.Add(" 出错!!!!!!\t\t" + ex.Message);
return null;
}
} private void button3_Click(object sender, EventArgs e)
{
Stop = true;
} private void SearchFiles(string path, List<Filees> A, List<Filees> B)
{
foreach (string item in Directory.GetFiles(path))
{
if (item.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) && !item.Contains("Designer"))
{
Filees temp = new Filees();
temp.FullName = item;
temp.FileName = GetFileName(item);
temp.ModifyTime = File.GetLastWriteTime(item);
A.Add(temp);
//WriteToTxt("D:\\1.txt", temp.FileName, temp.ModifyTime.ToString("yyyy-MM-dd HH:mm"));
//更新B
bool b = false;
foreach (Filees items in B.ToArray())
{
if (items.FileName == temp.FileName)
{
b = true;
if (items.ModifyTime < temp.ModifyTime)
{
B.Remove(items);
B.Add(temp);
}
}
}
if (!b)
B.Add(temp);
}
}
foreach (string item in Directory.GetDirectories(path))
{
SearchFiles(item, A, B);
}
} void WriteToTxt(string path, string text, string time)
{
using (StreamWriter sw = new StreamWriter(path, true))
{
sw.WriteLine(text + "\t" + time);
sw.Close();
}
} /// <summary>
/// 获取指定文件后缀名
/// </summary>
/// <param name="str">文件</param>
/// <returns>待返回的路径名</returns>
private string GetSuffix(string str)
{
string temp = str.Trim();
if (string.IsNullOrEmpty(temp))
return "";
temp = GetFileName(temp);
int i = temp.LastIndexOf('.');
if (i > -)
return temp.Substring(i + );
return "";
} /// <summary>
/// 获取指定路径文件名
/// </summary>
/// <param name="str">路径</param>
/// <returns>待返回的文件名</returns>
private string GetFileName(string str)
{
if (string.IsNullOrEmpty(str))
return "";
int i = str.LastIndexOf('\\');
return str.Substring(i + );
} /// <summary>
/// 获取字符串中路径
/// </summary>
/// <param name="str">字符串</param>
/// <returns>待返回的路径</returns>
private string GetFullPath(string str)
{
if (string.IsNullOrEmpty(str))
return "";
int i = str.LastIndexOf('\\');
return str.Substring(, i);
} private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
} private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
// 此代码有错,当出现滚动条时 就会选错
if (e.Button == MouseButtons.Right)
{
int i = e.Y / listBox1.ItemHeight;
listBox1.SelectedIndex = i < listBox1.Items.Count ? i : listBox1.Items.Count - ;
}
} private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.Clear();
Clipboard.SetText("第 " + (listBox1.SelectedIndex + ).ToString() + " 行\t" + listBox1.SelectedItem.ToString());
} private void toTxtToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog fd = new SaveFileDialog();
fd.FileName = "D:\\1.txt";
fd.Filter = "文本文件|*.txt|全部文件|*.*";
if (fd.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(fd.FileName, false))
{
foreach (string item in listBox1.Items)
{
sw.WriteLine(item);
}
sw.Close();
}
}
} private void listBox1_DoubleClick(object sender, EventArgs e)
{
try
{
Process.Start("Explorer.exe", GetFullPath(listBox1.SelectedItem.ToString()));
}
catch
{
MessageBox.Show(this, "这个路径不合法!\r\n\r\n无法打开所在文件夹!", "提示");
}
} private void button4_Click(object sender, EventArgs e)
{
// 扫描文件夹中 DLL 位置 和 修改时间 并保存到两个表
// 一个表里保存的全部
// 另一表里保存的最新修改时间的 DLL 位置和时间 //私有变量 目的是减少内存占用 或者说减少出错。
List<Filees> A = new List<Filees>();
List<Filees> B = new List<Filees>();
// 切莫忘了清空 如果不需要即时清空 也要添加右键菜单的清空操作.
listBox1.Items.Clear();
string path = textBox1.Text.Trim();
if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
{
SearchFiles(path, A, B);
}
bool checkVer = checkBox1.Checked;
foreach (Filees item in A.ToArray())
{
foreach (Filees items in B.ToArray())
{
if (item.FileName == items.FileName)
{
if (!checkVer || (checkVer && ver(item.FullName) == ver(items.FullName))) //检查版本号
{
if (File.GetLastWriteTime(item.FullName) < File.GetLastWriteTime(items.FullName))
{
listBox1.Items.Add(item.FullName + "\t修改时间:" + item.ModifyTime + "\t最新时间:" + items.ModifyTime);
}
}
}
}
}
if (listBox1.Items.Count == )
listBox1.Items.Add("已扫描完成,Dll无需更新!");
}
}
}

自动更新文件夹下所有DLL 至最新修改时间版本的更多相关文章

  1. MapReduce会自动忽略文件夹下的.开头的文件

    MapReduce会自动忽略文件夹下的.开头的文件,跳过这些文件的处理.

  2. php自动读取文件夹下所有图片

    $path = 'xxxxx';///当前目录$handle = opendir($path); //当前目录while (false !== ($file = readdir($handle))) ...

  3. 引用AForge.video.ffmpeg,打开时会报错:找不到指定的模块,需要把发行包第三方文件externals\ffmpeg\bin里的dll文件拷到windows的system32文件夹下。

    引用AForge.video.ffmpeg,打开时会报错:找不到指定的模块,需要把发行包第三方文件externals\ffmpeg\bin里的dll文件拷到windows的system32文件夹下. ...

  4. MVC写在Model文件夹下,登录注册等页面定义的变量规则,不会被更新实体模型删除

    一下图为我的model文件夹

  5. python——在文件存放路径下自动创建文件夹!

    1.a.py文件存放的路径下为(D:\Auto\eclipse\workspace\Testhtml\Test) 2.通过os.getcwd()获取的路径为:D:\Auto\eclipse\works ...

  6. Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本

    Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本 在 mac 或者 linux 系统中,我们的浏览器或者其他下载软件下载的文件全部都下载再 ~/Downloads/ 文 ...

  7. webform工程中aspx页面为何不能调用appcode文件夹下的类(ASP.NET特殊文件夹的用法)

    App_code 只有website类型的工程才有效. App_Code 下创建的.cs文件仅仅是“内容”不是代码.你设置那个文件为“编译”就行了. 其他特殊文件夹 1. Bin文件夹 Bin文件夹包 ...

  8. GreenDao 数据库:使用Raw文件夹下的数据库文件以及数据库升级

    一.使用Raw文件夹下的数据库文件 在使用GreenDao框架时,数据库和数据表都是根据生成的框架代码来自动创建的,从生成的DaoMaster中的OpenHelper类可以看出: public sta ...

  9. 脚本工具(获取某个文件夹下的所有图片属性批量生成css样式)

    问题描述: 由于有一次工作原因,就是将某个文件夹下的所有图片,通过CSS描述他们的属性,用的时候就可以直接引用.但是我觉得那个文件夹下的图片太多,而且CSS文件的格式又有一定的规律,所有想通过脚本来生 ...

随机推荐

  1. You are using the runtime-only build of Vue where the template compiler is not available.

    使用vue-cli搭建的项目,启动报错 You are using the runtime-only build of Vue where the template compiler is not a ...

  2. Spring Cloud(八)高可用的分布式配置中心 Spring Cloud Config

    在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config,它支持配 ...

  3. 【Struts2】拦截器

    一.概述 二.在Struts2中使用拦截器 2.1 步骤 2.2 分析拦截器原理 2.3 关于interceptor与Filter区别: 三.案例 一.概述 介绍拦截器: struts2拦截器使用的是 ...

  4. 【Mac】 开启原生的 NTFS 硬盘格式支持

    一.MacOS 10.13 之前 二.MacOS 10.13 及之后 一.MacOS 10.13 之前 直接跳到引用地址查看,下面的草记只是为了防止链接丢失 引用地址 打开终端 切换至root身份,输 ...

  5. Packet for query is too large (4,544,730 > 4,194,304). You can change this value on the server by setting the 'max_allowed_packet' variable.

    修改 my.ini 加上 max_allowed_packet =6710886467108864=64M默认大小4194304  也就是4M修改完成之后要重启mysql服务,如果通过命令行修改就不用 ...

  6. [Abp vNext微服务实践] - vue-element-admin管理Identity

    一.简介 abp vNext微服务框架中已经提供通用权限和用户角色管理模块,管理UI使用的是MVC模式,不适用于国内主打的MVVM开发模式.在前端框架选型后笔者决定改造abp vNext微服务框架中原 ...

  7. Python&Selenium&pytest借助allure生成自动化测试报告

    一.摘要 本篇博文将介绍Python和Selenium进行自动化测试时,如何借助allure生成自动化测试报告 二.环境配置 首先python环境中安装pytest和pytest_allure_ada ...

  8. FFmpeg常用命令学习笔记(八)滤镜相关命令

    滤镜相关命令 FFmpeg中关于滤镜命令有很多种.比如在视频中加入/删除水印.对视频做反转等等都属于滤镜. FFmpeg滤镜处理流程 解码后的数据帧 ---过滤处理---> 过滤后的数据帧 -- ...

  9. BZOJ2208 [Jsoi2010]连通数[缩点/Floyd传递闭包+bitset优化]

    显然并不能直接dfs,因为$m$会非常大,复杂度就是$O(mn)$: 这题有三种做法,都用到了bitset的优化.第二种算是一个意外的收获,之前没想到竟然还有这种神仙操作.. 方法一:缩点+DAG上b ...

  10. modbus-RTU-crc16——c语言

    为确保消息数据的完整性,除了验证消息CRC之外,建议实现检查串行端口(UART)成帧错误的代码.如果接收消息中的CRC与接收设备计算的CRC不匹配,则应忽略该消息.下面的C语言代码片段显示了如何使用逐 ...