using FLY.OBJComponents.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace FLY.Winder.UI.Server
{
///
/// WdSetup.xaml 的交互逻辑
///
public partial class WdSetup : Window
{
public WdSetup()
{
InitializeComponent();
Init();
}
private void Init()
{
var items = new List();
//查找 脚本/称重 下的全部 设备连接变量表_xxx 的文件夹
DirectoryInfo directoryInfo = new DirectoryInfo(@"plcgroups");
if (!directoryInfo.Exists)
{
//异常
return;
}
var dirs = directoryInfo.GetDirectories();
foreach (var dir in dirs)
{
if (dir.Name.StartsWith("设备连接变量表_"))
{
PlcGroupItem item = new PlcGroupItem();
item.Name = dir.Name.Substring("设备连接变量表_".Length);
item.Path = System.IO.Path.Combine(dir.FullName, "Generated", "plcgroup.json");
items.Add(item);
}
}
comboBox.ItemsSource = items;
foreach (var item in items)
{
if (IsSameContent(item.Path, "plcgroup.json"))
{
//找到了
comboBox.SelectedItem = item;
break;
}
}
}
public static bool IsSameContent(string filePath1, string filePath2)
{
//创建一个哈希算法对象
using (HashAlgorithm hash = HashAlgorithm.Create())
{
using (FileStream file1 = new FileStream(filePath1, FileMode.Open), file2 = new FileStream(filePath2, FileMode.Open))
{
byte[] hashByte1 = hash.ComputeHash(file1);//哈希算法根据文本得到哈希码的字节数组
byte[] hashByte2 = hash.ComputeHash(file2);
return Enumerable.SequenceEqual(hashByte1, hashByte2);
//string str1 = BitConverter.ToString(hashByte1);//将字节数组装换为字符串
//string str2 = BitConverter.ToString(hashByte2);
//return (str1 == str2);//比较哈希码
}
}
}
private void btnOkClick(object sender, RoutedEventArgs e)
{
var item = comboBox.SelectedItem as PlcGroupItem;
if (item == null) {
MessageBox.Show("请选择型号!!!");
return;
}
if (MessageBox.Show("需要重启,才能生效", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK) {
File.Copy(item.Path, "plcgroup.json", true);
//System.Windows.Forms.Application.Restart();
Application.Current.Shutdown();
}
}
}
public class PlcGroupItem
{
///
/// 名称
///
public string Name { get; set; }
///
/// 路径
///
public string Path { get; set; }
}
}