using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WFormReadData_SMR { public partial class EquipmentCollectionForm : Form { private json objJsonResult = new json(); DataTable dt = new DataTable(); public EquipmentCollectionForm() { InitializeComponent(); } private void EquipmentCollectionForm_Load(object sender, EventArgs e) { this.txtBegin.ReadOnly = true; this.txtEnd.ReadOnly = true; this.txtLj.ReadOnly = true; this.btnSave.Enabled = false; } private async void btnBegin_Click(object sender, EventArgs e) { if (this.txtHBathNo.Text == "" || this.txtHBathNo.ReadOnly == false) { MessageBox.Show("请输入批次号进行确认!"); } else { await getUrl(); this.txtBegin.Text = DateTime.Now.ToString(); this.btnBegin.Enabled = false; } } private void btnEnd_Click(object sender, EventArgs e) { if (this.txtBegin.Text == "") { MessageBox.Show("请点击开始按钮进行开始确认!"); } else { this.txtEnd.Text = DateTime.Now.ToString(); this.btnEnd.Enabled = false; } } private void btnLj_Click(object sender, EventArgs e) { if (this.txtEnd.Text == "") { MessageBox.Show("请点击结束按钮进行结束确认!"); } else { //路径赋值给文本 using (OpenFileDialog openFile = new OpenFileDialog()) { DialogResult result = openFile.ShowDialog(); if (result == DialogResult.OK) { //保存按钮灰掉 this.btnSave.Enabled = true; string selectedFolderPath = openFile.FileName; this.txtLj.Text = selectedFolderPath; objJsonResult = Xt_CSVReadText(selectedFolderPath, 1); dt = objJsonResult.dataTable; if (objJsonResult.code == "0") { MessageBox.Show(objJsonResult.Message); } else { for (int i = 0; i < dt.Rows.Count; i++) { ListData.Items.Add("底孔直径:" + dt.Rows[i]["BottomHoleDiameter"].ToString() + ",有无螺牙检测数据:" + dt.Rows[i]["TestingData"].ToString() + ",有无螺牙检测结果:" + dt.Rows[i]["DetectionResult"].ToString()); } } } } } } //读取CSV文件数据 根据文件路径找到对应文件 并获取对应的数据 public json Xt_CSVReadText(string FilePath, int num) { try { DataTable dt = new DataTable(num.ToString()); dt.Columns.Add("BottomHoleDiameter"); dt.Columns.Add("TestingData"); dt.Columns.Add("DetectionResult"); DataTable dt2 = new DataTable(); List records = new List(); using (StreamReader sr = new StreamReader(FilePath, Encoding.GetEncoding("gb2312"))) { string[] headers = sr.ReadLine()?.Split(','); if (headers != null) { foreach (var header in headers) { dt2.Columns.Add(header.Trim()); } } string line; while ((line = sr.ReadLine()) != null) { var values = line.Split(','); DataRow row = dt2.NewRow(); for (int i = 0; i < values.Length; i++) { // 这里可能需要添加类型转换和错误处理 row[i] = values[i].Trim(); } dt2.Rows.Add(row); } } for (int i = 0; i < dt2.Rows.Count; i++) { for (int j = 0; j < 2; j++) { DataRow dr = dt.NewRow(); dr["BottomHoleDiameter"] = dt2.Rows[i][dt2.Columns[j]].ToString(); dr["TestingData"] = dt2.Rows[i][dt2.Columns[j + 2]].ToString(); dr["DetectionResult"] = dt2.Rows[i][dt2.Columns[j + 4]].ToString(); dt.Rows.Add(dr); } } objJsonResult.count = 1; objJsonResult.Message = ""; objJsonResult.dataTable = dt; return objJsonResult; } catch (Exception e) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = e.Message; objJsonResult.dataTable = null; return objJsonResult; } } private void txtHBathNo_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { this.txtHBathNo.ReadOnly = true; } } private void btnRest_Click(object sender, EventArgs e) { this.txtHBathNo.Text = ""; this.txtHBathNo.ReadOnly = false; this.btnBegin.Enabled = true; this.txtBegin.Text = ""; this.btnEnd.Enabled = true; this.txtEnd.Text = ""; this.btnSave.Enabled = false; this.txtLj.Text = ""; } private void btnSave_Click(object sender, EventArgs e) { DialogResult dr = MessageBox.Show("确认保存?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dr == DialogResult.Yes) { var formData = new FormUrlEncodedContent(new[]{ new KeyValuePair("userName", "admin"), new KeyValuePair("password", "admin") // 添加更多键值对,根据您的需要 }); } } //string url, FormUrlEncodedContent formData static async Task getUrl() { // 创建 HttpClient 实例 using (var client = new HttpClient()) { // 设置请求地址 string url = "https://preview.nps.iiot.youngsunnb.com/api/third/findAccessToken"; // 准备要发送的表单数据 var formData = new FormUrlEncodedContent(new[] { new KeyValuePair("userName", "admin"), new KeyValuePair("password", "admin") // 添加更多键值对,根据您的需要 }); // 发送 POST 请求,并等待响应 HttpResponseMessage response = await client.PostAsync(url, formData); // 检查响应是否成功 if (response.IsSuccessStatusCode) { // 处理成功响应的逻辑 string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine("响应内容:" + responseBody); } else { // 处理失败响应的逻辑 Console.WriteLine("请求失败,状态码:" + response.StatusCode); } } } } }