自动更新文件夹下所有DLL 至最新修改时间版本
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 至最新修改时间版本的更多相关文章
- MapReduce会自动忽略文件夹下的.开头的文件
MapReduce会自动忽略文件夹下的.开头的文件,跳过这些文件的处理.
- php自动读取文件夹下所有图片
$path = 'xxxxx';///当前目录$handle = opendir($path); //当前目录while (false !== ($file = readdir($handle))) ...
- 引用AForge.video.ffmpeg,打开时会报错:找不到指定的模块,需要把发行包第三方文件externals\ffmpeg\bin里的dll文件拷到windows的system32文件夹下。
引用AForge.video.ffmpeg,打开时会报错:找不到指定的模块,需要把发行包第三方文件externals\ffmpeg\bin里的dll文件拷到windows的system32文件夹下. ...
- MVC写在Model文件夹下,登录注册等页面定义的变量规则,不会被更新实体模型删除
一下图为我的model文件夹
- python——在文件存放路径下自动创建文件夹!
1.a.py文件存放的路径下为(D:\Auto\eclipse\workspace\Testhtml\Test) 2.通过os.getcwd()获取的路径为:D:\Auto\eclipse\works ...
- Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本
Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本 在 mac 或者 linux 系统中,我们的浏览器或者其他下载软件下载的文件全部都下载再 ~/Downloads/ 文 ...
- webform工程中aspx页面为何不能调用appcode文件夹下的类(ASP.NET特殊文件夹的用法)
App_code 只有website类型的工程才有效. App_Code 下创建的.cs文件仅仅是“内容”不是代码.你设置那个文件为“编译”就行了. 其他特殊文件夹 1. Bin文件夹 Bin文件夹包 ...
- GreenDao 数据库:使用Raw文件夹下的数据库文件以及数据库升级
一.使用Raw文件夹下的数据库文件 在使用GreenDao框架时,数据库和数据表都是根据生成的框架代码来自动创建的,从生成的DaoMaster中的OpenHelper类可以看出: public sta ...
- 脚本工具(获取某个文件夹下的所有图片属性批量生成css样式)
问题描述: 由于有一次工作原因,就是将某个文件夹下的所有图片,通过CSS描述他们的属性,用的时候就可以直接引用.但是我觉得那个文件夹下的图片太多,而且CSS文件的格式又有一定的规律,所有想通过脚本来生 ...
随机推荐
- 使用LEANGOO泳道
转自:https://www.leangoo.com/leangoo_guide/leangoo_yongdao.html 列表使用纵向的纬度管理卡片,通常代表卡片的工作的不同阶段,或者任务的状态.泳 ...
- 内核开机logo
安装 # sudo apt-getinstall netpbm 制图 # bmptoppm logo.bmp > logo.ppm # ppmquant 224 logo.ppm > lo ...
- String s=new String("xyz");创建了几个String Object?二者之前的区别是什么?
两个.第一个对象是字符串常量"xyz",第二个对象是new String("xyz")的时候产生的,在堆中分配内存给这个对象,只不过这个对象的内容是指向字符串常 ...
- Linux基础篇之CentOS的网络配置(DHCP,静态)
1.启动系统,使用用户名.密码登录系统: 2. 配置网卡(DHCP获取IP地址.静态手动配置IP地址): 网卡的默认信息 DHCP模式修改为(下图): 静态IP地址修改为(下图): 无论哪种配置, ...
- c#中打开视频,word
打开所有的文件的 代码,包括word/ppt/txt/xls/mp3/视频文件 添加using using System.Diagnostics; string fileName = @"D ...
- tensorflow模型的保存与恢复,以及ckpt到pb的转化
转自 https://www.cnblogs.com/zerotoinfinity/p/10242849.html 一.模型的保存 使用tensorflow训练模型的过程中,需要适时对模型进行保存,以 ...
- 算法笔记--CDQ分治 && 整体二分
参考:https://www.luogu.org/blog/Owencodeisking/post-xue-xi-bi-ji-cdq-fen-zhi-hu-zheng-ti-er-fen 前置技能:树 ...
- 关于Java的Object.clone()方法与对象的深浅拷贝
文章同步更新在个人博客:关于Java的Object.clone()方法与对象的深浅拷贝 引言 在某些场景中,我们需要获取到一个对象的拷贝用于某些处理.这时候就可以用到Java中的Object.clon ...
- JavaScript教程——实例对象与 new 命令
典型的面向对象编程语言(比如 C++ 和 Java),都有“类”(class)这个概念.所谓“类”就是对象的模板,对象就是“类”的实例.但是,JavaScript 语言的对象体系,不是基于“类”的,而 ...
- Linux学习日志(一)
1 .Ubuntu 自带python 2 和 python 3的版本,切换方法如下: shell里执行: sudo update-alternatives --install /usr/bin/pyt ...