/// <summary>
/// 打开本地对比文件夹路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpenPath_Click(object sender, EventArgs e)
{
System.Windows.Forms.FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择要对比的本地客户端所在的文件夹";
this.btnOpenPath.Text = "对比中...";
if (dialog.ShowDialog() == DialogResult.OK)
{
DataTable dtselect = new DataTable();
string path = dialog.SelectedPath;
DirectoryInfo root = new DirectoryInfo(path);
System.IO.FileInfo[] files = root.GetFiles("*.*", SearchOption.AllDirectories);
//progressBar1.Maximum = files.Length;
FormComparClientResult frm = new FormComparClientResult();
frm.FilePath = path;
// 使用 DataView 进行排序
DataView dataView = dtFileUpload.DefaultView;
dataView.RowFilter = "IS_CURVERSION=1";
dataView.Sort = "FILE_NAME ASC"; // 升序排序
frm.Hospitalfiles = dataView.ToTable();
DataTable dtLocal = dtFileUpload.Clone();
for (int i = 0; i < files.Length; i++)
{
System.IO.FileInfo file = files[i];
//progressBar1.Value = i;
string extension = Path.GetExtension(file.FullName);
if (extension.ToLower() == ".dll" || extension.ToLower() == ".exe")
{
DataRow dr = dtLocal.NewRow();
dr["FILE_NAME"] = file.Name;
dr["VERSION_NAME"] = FileVersionInfo.GetVersionInfo(file.FullName).FileVersion;
dr["UPLOAD_DATE"] = file.LastWriteTime;
dr["LAST_UPDATE"] = file.LastWriteTime;
if (files[i].FullName.ToLower().Contains("widll"))
{
dr["PATH"] = @"\widll\";
}
else
{
dr["PATH"] = @"\";
}
dtLocal.Rows.Add(dr);
}
}
DataView dataViewLocal = dtLocal.DefaultView;
dataViewLocal.Sort = "FILE_NAME ASC"; // 升序排序
frm.Selectedfiles = dataViewLocal.ToTable();
this.btnOpenPath.Text = "选择客户端";
frm.ShowDialog();
}
}
/// <summary>
/// 导出对比excel,和复制对比结果不同的dll
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExportExcel_Click(object sender, EventArgs e)
{
DataTable dtselefile = selectedfiles.Copy();
DataView dvselefile = dtselefile.DefaultView;
dvselefile.RowFilter = "STATUS<>'0'";
dtselefile = dvselefile.ToTable();
DirectoryInfo root = new DirectoryInfo(filePath);
System.IO.FileInfo[] files = root.GetFiles("*.*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
if (dtselefile.Select($"FILE_NAME='{files[i].Name}'").Length > 0)
{
string strDes = files[i].FullName.ToLower().Contains("widget") ? "\\COPY\\widget" : "\\COPY";
CopyToFile(files[i].FullName, filePath + "\\COPY");
}
}
ExportExcel(gridControlLocal);
}
/// <summary>
/// 拷贝文件到另一个文件夹下
/// </summary>
/// <param name="sourceName">源文件路径</param>
/// <param name="folderPath">目标路径(目标文件夹)</param>
public void CopyToFile(string sourceName, string folderPath)
{
//例子:
//源文件路径
//string sourceName = @"D:\Source\Test.txt";
//目标路径:项目下的NewTest文件夹,(如果没有就创建该文件夹)
//string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NewTest");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
//当前文件如果不用新的文件名,那么就用原文件文件名
string fileName = Path.GetFileName(sourceName);
//这里可以给文件换个新名字,如下:
//string fileName = string.Format("{0}.{1}", "newFileText", "txt");
//目标整体路径
string targetPath = Path.Combine(folderPath, fileName);
//Copy到新文件下
FileInfo file = new FileInfo(sourceName);
if (file.Exists)
{
//true 为覆盖已存在的同名文件,false 为不覆盖
FileInfo fileCopy = new FileInfo(targetPath);
if (!fileCopy.Exists)
{
file.CopyTo(targetPath, false);
}
}
}