using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
//using System.Text;
using MT8852B_Driver;
using System.IO;
namespace NRF52382_Test
{
public partial class FormConfig : Form
{
FormMain frmMain = new FormMain();
public void LoadXMLToDataGridView()
{
XmlTextReader xml = null;
DataTable dt = null;
string strMsg = "";
try
{
dt = new DataTable();
//添加列名
dt.Columns.Add("TestCase", typeof(string));
dt.Columns.Add("Parameter", typeof(string));
dt.Columns.Add("Enable", typeof(string));
//dt.Columns.Add("Description", typeof(string));
string strJsonfile = System.Environment.CurrentDirectory + "\\TestSrcipt.xml";
xml = new XmlTextReader(strJsonfile);
xml.WhitespaceHandling = WhitespaceHandling.None;
while (xml.Read())
{
if (xml.NodeType == XmlNodeType.Element)
{
if (xml.Name == "COM")
{
tbCOM.Text = xml.ReadString();
}
if(xml.Name == "GPIB_Index")
{
List<string> ls = new List<string>();
string strTmp = xml.GetAttribute("Elements");
List<string> list = new List<string>(strTmp.Split(','));
list.ForEach(i => ls.Add(i));
string strCurrVaule = xml.ReadString();
ls.ForEach(x => cbGpibIndex.Items.Add(x.ToString()));
cbGpibIndex.SelectedIndex = cbGpibIndex.Items.IndexOf(strCurrVaule);
//cbGpibIndex.Text = xml.ReadString();
}
if (xml.Name == "GPIB_Address")
{
tbGpibAddress.Text = xml.ReadString();
}
if (xml.Name == "TestCase1" || xml.Name == "TestCase2" || xml.Name == "TestCase3" || xml.Name == "TestCase4" || xml.Name == "TestCase5" || xml.Name == "TestCase6" || xml.Name == "TestCase7" || xml.Name == "TestCase8" || xml.Name == "TestCase9")
{
DataRow dr = dt.NewRow();
string strCaseName = xml.GetAttribute("Name");
dr["TestCase"] = strCaseName;
string strVaule = xml.GetAttribute("Value");
dr["Parameter"] = strVaule;
string strEnable = xml.GetAttribute("Enable");
dr["Enable"] = strCaseName;
dt.Rows.Add(dr);
}
}
}
//创建DataSet对象,绑定数据
DataSet ds = new DataSet();
ds.Tables.Add(dt);
dataGridView1.DataSource = dt;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
}
catch (XmlException xmlEx)
{
strMsg = xmlEx.Message.ToString();
MessageBox.Show(strMsg);
}
catch (DataException dataEx)
{
strMsg = dataEx.Message.ToString();
MessageBox.Show(strMsg);
}
catch (SystemException sysEx)
{
strMsg = sysEx.Message.ToString();
MessageBox.Show(strMsg);
}
finally
{
xml.Close();
}
}
public void SaveXMLFromDataGridView()
{
string strTmp = "";
string strMsg = "";
string strJsonfile = System.Environment.CurrentDirectory + "\\TestSrcipt.xml";
XmlDocument xml = null;
try
{
xml = new XmlDocument();
xml.Load(strJsonfile);
XmlElement pXmlElement = xml.SelectSingleNode(@"configuration/BtTest/Init/COM") as XmlElement;
pXmlElement.InnerText = tbCOM.Text;
pXmlElement = xml.SelectSingleNode(@"configuration/BtTest/Init/GPIB_Index") as XmlElement;
pXmlElement.InnerText = cbGpibIndex.Text;
pXmlElement = xml.SelectSingleNode(@"configuration/BtTest/Init/GPIB_Address") as XmlElement;
pXmlElement.InnerText = tbGpibAddress.Text;
for (int i = 0; i < 9; i++)
{
string strTmp2 = string.Format("configuration/BtTest/TestCase/TestCase{0}", (i + 1));
pXmlElement = xml.SelectSingleNode(strTmp2) as XmlElement;
strTmp = dataGridView1["Parameter", i].Value.ToString();
pXmlElement.SetAttribute("Value", strTmp);
//pXmlElement.InnerText = strTmp;
}
xml.Save(strJsonfile);
MessageBox.Show("数据保存成功!");
this.Close();
}
catch (XmlException xmlEx)
{
strMsg = xmlEx.Message.ToString();
MessageBox.Show(strMsg);
}
catch (DataException dataEx)
{
strMsg = dataEx.Message.ToString();
MessageBox.Show(strMsg);
}
catch (SystemException sysEx)
{
strMsg = sysEx.Message.ToString();
MessageBox.Show(strMsg);
}
finally
{
}
}
public FormConfig()
{
InitializeComponent();
LoadXMLToDataGridView();
}
public FormConfig(FormMain formMain)
{
frmMain = formMain;
InitializeComponent();
}
private void btSave_Click(object sender, EventArgs e)
{
SaveXMLFromDataGridView();
}
#region 直接删除指定目录下的所有文件及文件夹(保留目录)
public static void DeleteDir(string file)
{
try
{
//去除文件夹和子文件的只读属性
//去除文件夹的只读属性
System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file);
fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
//去除文件的只读属性
System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);
//判断文件夹是否还存在
if (Directory.Exists(file))
{
foreach (string f in Directory.GetFileSystemEntries(file))
{
if (File.Exists(f))
{
//如果有子文件删除文件
File.Delete(f);
Console.WriteLine(f);
}
else
{
//循环递归删除子文件夹
DeleteDir(f);
}
}
//删除空文件夹
//Directory.Delete(file);
Console.WriteLine(file);
}
}
catch (Exception ex) // 异常处理
{
Console.WriteLine(ex.Message.ToString());// 异常信息
}
}
#endregion
//public delegate void ShowStatedelegate(string txt);//定义一个委托
//public event mydelegate myevent;
private void btReportClear_Click(object sender, EventArgs e)
{
//清除文件夹的所有文件
DeleteDir(System.Environment.CurrentDirectory + "\\Log\\");
DeleteDir(System.Environment.CurrentDirectory + "\\CSV\\");
frmMain.tbPass.Text = "0";
frmMain.tbFail.Text = "0";
frmMain.tbTotal.Text = "0";
frmMain.tbFYP.Text = "100%";
frmMain.ResetFYP();
}
private void FormConfig_Load(object sender, EventArgs e)
{
LoadXMLToDataGridView();
FormLogin frmLogin = new FormLogin();
frmLogin.ShowDialog();
if (frmLogin.DialogResult == DialogResult.OK)//如果登录框返回DialogResult.OK
{
//MessageBox.Show("正常登录");
}
else
{
this.Close();
}
}
}
}