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. B站视频下载

    借助Chrome插件 bilibili哔哩哔哩下载助手 在谷歌应用商城下载安装后在在浏览器右上角显示如下图标 打开想要下载的视频,网页右下角会有如下图标,点击该图标 点击下面的合并下载按钮即可 htt ...

  2. MySQL跨表更新SQL

    1 sql范式  把s表中的city_name的值设置为city表中的name,关联条件是city_code 和 code update student s, city c set s.city_na ...

  3. TLV320AIC3268寄存器读写

    该芯片支持I2C和SPI读写寄存器,本人用的是SPI1接口. 以下是对手册中SPI接口读写寄存器相关内容的翻译(英文版可以看手册的94页~) 在SPI控制模式下,TLV320AIC3268使用SCL_ ...

  4. swagger2注解使用方法

    swagger注解整体说明: @Api:用在请求的类上,表示对类的说明 tags="说明该类的作用,可以在UI界面上看到的注解" value="该参数没什么意义,在UI界 ...

  5. IDEA intellij 引用jar包方法

    以下为方法之一. 1. 点击左上角文件 - Project Structure 2. 依次按照下图点击 3. 选择需要导入的jar包,点击ok

  6. CentOS7 基于 subversion 配置 SVN server

    由于 Window Server 环境下,VisualSVN Server Community 版本只支持 15 个同时在线用户,所以彻底放弃 Windows Server,在 Linux Serve ...

  7. xgboost&lightgbm调参指南

    本文重点阐述了xgboost和lightgbm的主要参数和调参技巧,其理论部分可见集成学习,以下内容主要来自xgboost和LightGBM的官方文档. xgboost Xgboost参数主要分为三大 ...

  8. SpringMVC——问题汇总

    1.html页面ajax请求/login.后台报: Request method 'GET' not supported 原因: 后台请求使用了method = POST,但是url请求没有formd ...

  9. Linux(Ubuntu)安装ssh服务

    在终端(Ctrl + Alt + T )输入 $ps -e | grep ssh 看到 “ ssh-agent ” 和 “sshd” ,表示没有安装服务,或没有开机启动 1.安装SSH 输入:sudo ...

  10. C# ado.net 使用task和await(四)

    class Program { private static string constr = "server=.;database=northwnd;integrated security= ...