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. LEANGOO成员

    转自:https://www.leangoo.com/leangoo_guide/leangoo_guide_member.html 1. 看板成员及权限 一个看板上的最大成员限制为200个. 看板的 ...

  2. 前端基础(六):Bootstrap框架

    Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序而设计的一套前端工具包. 它支持响应式布局,并且 ...

  3. (6)python基础数据类型

    python的六大标准数据类型 (一)Number   数字类型(int float bool complex) (二)String 字符串类型 (三)List 列表类型 (四)Tuple 元组类型 ...

  4. 7. Function Decorators and Closures

    A decorator is a callable that takes another function as argument (the decorated function). The deco ...

  5. C#的Lazy与LazyInitializer

    class Program { static void Main(string[] args) { //初始化 Lazy 类的新实例 //当延迟初始化发生时,将使用指定的初始化函数和初始化模式 // ...

  6. java——spring中bean的作用域

    文章:理解Spring框架中Bean的作用域 博客地址:https://baijiahao.baidu.com/s?id=1610298792072480906&wfr=spider& ...

  7. okhttp拦截器之ConnectInterceptor解析

    主流程分析: 继续分析okhttp的拦截器,继上次分析了CacheInterceptor缓存拦截器之后,接下来到连接拦截器啦,如下: 打开看一下它的javadoc: 而整个它的实现不长,如下: 也就是 ...

  8. 使用Xcode Instruments定位APP稳定性问题

    Xcode Instruments提供了各种各样的工具用来定位APP的各种稳定性问题.这里简单总结几个问题: 1. 内存泄漏 Xcode->Open Developer Tools->In ...

  9. MyBatis-10-多对一处理

    10.多对一处理 多对一: 多个学生,对应一个老师 对于学生这边而言,关联...多个学生,关联一个老师[多对一] 对于老师而言,集合,一个老师又很多学生[一对多] SQL: CREATE TABLE ...

  10. 01—mybatis开山篇

    什么是 MyBatis ?        MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.M ...