WdSetup.xaml.cs 3.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
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
{
    /// <summary>
    /// WdSetup.xaml 的交互逻辑
    /// </summary>
    public partial class WdSetup : Window
    {


        public WdSetup()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            var items = new List<PlcGroupItem>();

            //查找 脚本/称重 下的全部 设备连接变量表_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
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 路径
        /// </summary>
        public string Path { get; set; }
    }
}