using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Metis.AutoAnalysis { public partial class SelectSampleInfo : Form { public SelectSampleInfo() { InitializeComponent(); } private void SelectSampleInfo_Load(object sender, EventArgs e) { lvSampleInfo.Columns.Add("检测项目"); lvSampleInfo.Columns.Add("检验批号"); lvSampleInfo.Columns.Add("试批顺序号"); lvSampleInfo.Columns.Add("试样号"); lvSampleInfo.Columns.Add("收样时间"); lvSampleInfo.Columns.Add("牌号(钢级)"); LoadSampleInfoData(""); for (int i = 0; i < lvSampleInfo.Columns.Count; i++) { lvSampleInfo.Columns[i].Width = -2; } } private void LoadSampleInfoData(string condition) { string dir = ConfigurationManager.AppSettings["sampleinfo_dir"]; string[] files = GetLatestFiles(dir, 1); if (files.Length <= 0) { MessageBox.Show("指定文件夹下的文件不存在"); return; } lvSampleInfo.Items.Clear(); try { StreamReader file = File.OpenText(files[0]); JsonTextReader reader = new JsonTextReader(file); JArray arr = (JArray)JToken.ReadFrom(reader); foreach (var json in arr) { if(string.IsNullOrEmpty(condition) == false) { string sampleId = json["SAMPLEID"].ToString(); if (sampleId.Contains(condition) == false) continue; } ListViewItem item = new ListViewItem(); item.SubItems[0].Text = json["TESTCODE"].ToString(); item.SubItems.Add(json["SAMPLELOTNO"].ToString()); item.SubItems.Add(json["SAMPLEID"].ToString()); item.SubItems.Add(json["FD_SAMPLE_NO"].ToString()); item.SubItems.Add(json["RECVTIME"].ToString()); item.SubItems.Add(json["FD_SG_SIGN"].ToString()); lvSampleInfo.Items.Add(item); } reader.Close(); file.Close(); } catch (Exception ex) { MessageBox.Show("读取json文件出错 " + ex.Message); } } public string TESTCODE { get; set; } public string SAMPLELOTNO { get; set; } public string SAMPLEID { get; set; } public string FD_SAMPLE_NO { get; set; } public string RECVTIME { get; set; } public string FD_SG_SIGN { get; set; } private string[] GetLatestFiles(string dir, int count) { try { var query = (from f in Directory.GetFiles(dir) let fi = new FileInfo(f) orderby fi.CreationTime descending select fi.FullName).Take(count); return query.ToArray(); } catch (Exception) { } return new string[0]; } private void btnSearch_Click(object sender, EventArgs e) { string cond = txtCond.Text; LoadSampleInfoData(cond); } private void btnClose_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } private void btnOK_Click(object sender, EventArgs e) { TESTCODE = lvSampleInfo.SelectedItems[0].SubItems[0].Text; SAMPLELOTNO = lvSampleInfo.SelectedItems[0].SubItems[1].Text; SAMPLEID = lvSampleInfo.SelectedItems[0].SubItems[2].Text; FD_SAMPLE_NO = lvSampleInfo.SelectedItems[0].SubItems[3].Text; RECVTIME = lvSampleInfo.SelectedItems[0].SubItems[4].Text; FD_SG_SIGN = lvSampleInfo.SelectedItems[0].SubItems[5].Text; this.DialogResult = DialogResult.OK; } } }