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无需更新!");
}
}
}