界面
代码
using System.Diagnostics;
namespace VersionTool
{
public partial class Form1 : Form
{
List<string> fileNmaes = new List<string>() { "PhotoMes.Base.dll", "PhotoMes.App.exe", "PhotoMes.Cameras.dll" };
public Form1()
{
InitializeComponent();
}
static bool CompareFiles(string path1, string path2)
{
using (var stream1 = File.OpenRead(path1))
using (var stream2 = File.OpenRead(path2))
{
byte[] buffer1 = new byte[4096];
byte[] buffer2 = new byte[4096];
while (true)
{
int bytesRead1 = stream1.Read(buffer1, 0, buffer1.Length);
int bytesRead2 = stream2.Read(buffer2, 0, buffer2.Length);
if (bytesRead1 != bytesRead2 || !BufferEquals(buffer1, buffer2, Math.Min(bytesRead1, bytesRead2)))
return false;
if (bytesRead1 == 0 && bytesRead2 == 0)
break;
}
}
return true;
}
static bool BufferEquals(byte[] buffer1, byte[] buffer2, int length)
{
for (int i = 0; i < length; ++i)
{
if (buffer1[i] != buffer2[i])
return false;
}
return true;
}
private void button3_Click(object sender, EventArgs e)
{
try
{
textBox3.Text = "";
foreach (var item in fileNmaes)
{
bool isSameFileContent = CompareFiles(textBox1.Text + @$"\{item}", textBox2.Text + @$"\{item}");
if (isSameFileContent)
textBox3.Text += item + " 版本相同";
else
{
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(textBox1.Text + @$"\{item}");
// 获取版本号
string version = fileVersionInfo.FileVersion;
fileVersionInfo = FileVersionInfo.GetVersionInfo(textBox2.Text + @$"\{item}");
// 获取版本号
string version2 = fileVersionInfo.FileVersion;
string[] sv1 = version.Split('.');
string[] sv2 = version2.Split('.');
if(version == version2)
{
textBox3.Text += item + " 版本不同,版本号相同";
}
else
{
for (int i = 0; i < sv1.Length; i++)
{
if (Convert.ToInt16(sv1[i]) > Convert.ToInt16(sv2[i]))
{
textBox3.Text += item + " 版本不同,路径1版本较新";
break;
}
else if (Convert.ToInt16(sv1[i]) < Convert.ToInt16(sv2[i]))
{
textBox3.Text += item + " 版本不同,路径2版本较新";
break;
}
}
}
}
}
}
catch (Exception ex)
{
textBox3.Text += ex.Message;
}
}
private void button1_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog DBO = new FolderBrowserDialog())
{
if (DBO.ShowDialog() == DialogResult.OK)
{
textBox1.Text = DBO.SelectedPath;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog DBO = new FolderBrowserDialog())
{
if (DBO.ShowDialog() == DialogResult.OK)
{
textBox2.Text = DBO.SelectedPath;
}
}
}
}
}