using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; using System.IO; using BaseLibRWFile; namespace HRT_Measure { public partial class FrmAddMaterial : Form { public FrmAddMaterial() { InitializeComponent(); } #region 控件 private void FrmAddMaterial_Load(object sender, EventArgs e) { //this.Size = FrmMain.sizeProject; splitContainer1.SplitterDistance = this.Width * 2 / 3; //iniAddMaterial.filePath = ClsVariable.defaultIniPath; //VisionHelp.Instance.ProConfig.StrCurrentPartNumber;//strSelectedValue = iniAddMaterial.ReadIni("CurrentProduct", "ProductName"); //asc.controllInitializeSize(this); CheckListBoxUpdate(); } private void btnUpdate_Click(object sender, EventArgs e)//更新 { CheckListBoxUpdate(); } private void btnSeleted_Click(object sender, EventArgs e)//选中 { if (MessageBox.Show("请确定是否更换料号?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { if(clbSeletedItem.SelectedItem.ToString() != ProgramHelp.Instance.ProConfig.当前料号) { //iniAddMaterial.filePath = ClsVariable.defaultIniPath; //iniAddMaterial.WriteIni("CurrentProduct", "ProductName", strSelectedValue); ProgramHelp.Instance.ProConfig.当前料号 = clbSeletedItem.SelectedItem.ToString(); lblCurrentProduct.Text = "当前料号为:" + ProgramHelp.Instance.ProConfig.当前料号; //加载相机等内容 //try //{ // CogCameraInitialize.Instance.CameraClose(); //} //catch (Exception ex) //{ // MessageBox.Show(ex.ToString()); //} //try //{ // CogCameraInitialize.Instance.Initialize(); //} //catch (Exception ex) //{ // MessageBox.Show(ex.ToString()); //} ProgramHelp.Instance.frmMain.ChangePartNumber(ProgramHelp.Instance.ProConfig.当前料号); } else { MessageBox.Show("更换料号不能为同一料号!!!"); } } } private void btnDelete_Click(object sender, EventArgs e)//删除 { if (MessageBox.Show("请确定是否删除料号?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { //iniAddMaterial.filePath = ClsVariable.defaultIniPath; if (clbSeletedItem.SelectedItem.ToString() != ProgramHelp.Instance.ProConfig.当前料号)//clsIniFile.IniReadValue("DefaultProduct", "ProductName") strSelectedValue != iniAddMaterial.ReadIni("CurrentProduct", "ProductName") { if (clbSeletedItem.SelectedItems.Count > 0) { //string strName = clbSeletedItem.SelectedItem.ToString();//获取当前选中项 //string strPath = ClsVariable.ProductsPath + "\\" + strName; FileOperate.DeleteFolder(Application.StartupPath + $"\\Config\\{clbSeletedItem.SelectedItem.ToString()}"); CheckListBoxUpdate(); MessageBox.Show("删除成功!!!"); } } else { MessageBox.Show("当前料号不允许删除!!!"); } } } private void btnAdd_Click(object sender, EventArgs e)//新增 { AddNewMaterial(ProgramHelp.Instance.ProConfig.当前料号, txtNewName.Text.Trim()); } private void btnReName_Click(object sender, EventArgs e)//重命名 { if (clbSeletedItem.SelectedIndex >= 0 && txtNewName.Text.Trim().Length > 0) ReName(clbSeletedItem.Items[clbSeletedItem.SelectedIndex].ToString(), txtNewName.Text.Trim()); else MessageBox.Show("请选择操作项或输入名称!"); } private void timer1_Tick(object sender, EventArgs e)//定时刷新 { //iniAddMaterial.filePath = ClsVariable.defaultIniPath; //lblCurrentProduct.Text = "当前料号为:" + iniAddMaterial.ReadIni("CurrentProduct", "ProductName");//clsIniFile.IniReadValue("DefaultProduct", "ProductName") / //ClsVariable.currentIniPath = iniAddMaterial.ReadIni("CurrentProduct", "ProductName");//更新当前料号 } #endregion #region 方法 public void CheckListBoxUpdate()//更新checkedListBox { clbSeletedItem.Items.Clear(); DirectoryInfo di = new DirectoryInfo(Application.StartupPath + "\\Config");//ClsVariable.ProductsPath DirectoryInfo[] subDirectories = di.GetDirectories();//返回当前目录的子目录 foreach (DirectoryInfo dir in subDirectories) { clbSeletedItem.Items.Add(dir.Name.ToString());//显示当前所有料号文件夹到checkedListbox里 } if (Directory.Exists(Application.StartupPath + $"\\Config\\{ProgramHelp.Instance.ProConfig.当前料号}"))//选中当前料号 ClsVariable.defaultIniPath { //iniAddMaterial.filePath = ClsVariable.defaultIniPath; //string currentName = iniAddMaterial.ReadIni("CurrentProduct", "ProductName"); bool setTag = false; for (int i = 0; i < clbSeletedItem.Items.Count; i++) { if (clbSeletedItem.Items[i].ToString() == ProgramHelp.Instance.ProConfig.当前料号) { clbSeletedItem.SetItemCheckState(i, CheckState.Checked);//设置指定索引处项的复选状态 setTag = true; lblCurrentProduct.Text = "当前料号为:" + ProgramHelp.Instance.ProConfig.当前料号; //ClsVariable.currentIniPath = iniAddMaterial.ReadIni("CurrentProduct", "ProductName");//更新当前料号 return; } } if (setTag == false) { //暂时不考虑 //clsIniFile.IniWriteValue("DefaultProduct", "ProductName", ""); } } } private void clbSeletedItem_ItemCheck(object sender, ItemCheckEventArgs e)//更改选中状态 { for (int i = 0; i < clbSeletedItem.Items.Count; i++) { if (i != e.Index) { clbSeletedItem.SetItemCheckState(i, CheckState.Unchecked); //设置指定索引处项的复选状态 } } //strSelectedValue = clbSeletedItem.Items[e.Index].ToString().Trim(); } public void AddNewMaterial(string currentName, string newName)//添加新料号 { try { if (newName != "")//newName不为空时创建文件夹 { string newPath = Application.StartupPath + "\\Config" + "\\" + newName; // ClsVariable.ProductsPath + "\\" + newName; if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath);//创建文件夹 MessageBox.Show("料号: " + newName + " 添加成功!"); if (currentName != "" && currentName != string.Empty)//选中某个料号进行复制 { string [] strFileName = Directory.GetFiles(Application.StartupPath + $"\\Config\\{currentName}"); foreach (string file in strFileName) //返回指定目录中文件的名称(包括其路径) { string strSource = Path.GetFileName(file); if (strSource.Contains($"{currentName}")) { string destPath = newPath + "\\" + strSource.Replace($"{currentName}", $"{newName}"); File.Copy(file, destPath, true); } } } } else { MessageBox.Show("料号添加失败,料号已存在!"); } //CopyConfigToNewFoleder(currentName, newName); //CopyCalToNewFoleder(currentName, newName); CheckListBoxUpdate(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } public void ReName(string selectName, string newName)//重命名 { try { if (selectName != "" && selectName != string.Empty) { if (selectName == ProgramHelp.Instance.ProConfig.当前料号)//clsIniFile.IniReadValue("DefaultProduct", "ProductName") { MessageBox.Show("当前料号不能重命名!"); } else { if (Directory.Exists(Application.StartupPath + "\\Config" + $"\\{selectName}")) { if (newName != "" && newName != string.Empty) { Directory.CreateDirectory(Application.StartupPath + "\\Config" + "\\" + newName);//新建文件夹 foreach (string file in Directory.GetFiles(Application.StartupPath + $"\\Config\\{selectName}")) //返回指定目录中文件的名称(包括其路径) { if (Path.GetFileName(file).Contains($"{selectName}")) { string destPath = Path.GetFileName(file).Replace($"{selectName}", $"{newName}"); File.Move(Path.GetFileName(file), destPath); } } FileOperate.DeleteFolder(Application.StartupPath + "\\Config" + $"\\{selectName}"); } } } CheckListBoxUpdate(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } #endregion public void CopyConfigToNewFoleder(string currentName, string newName) { if (newName != "")//newName不为空时创建文件夹 { string newPath = Application.StartupPath + "\\Config" + "\\" + newName; // ClsVariable.ProductsPath + "\\" + newName; if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath);//创建文件夹 //MessageBox.Show("料号: " + newName + " 添加成功!"); if (currentName != "" && currentName != string.Empty)//选中某个料号进行复制 { string[] strFileName = Directory.GetFiles(Application.StartupPath + $"\\Config\\{currentName}"); foreach (string file in strFileName) //返回指定目录中文件的名称(包括其路径) { string strSource = Path.GetFileName(file); if (strSource.Contains($"{currentName}")) { string destPath = newPath + "\\" + strSource.Replace($"{currentName}", $"{newName}"); File.Copy(file, destPath, true); } } } } else { MessageBox.Show("料号添加失败,料号已存在!"); } } } public void CopyCalToNewFoleder(string currentName, string newName) { if (newName != "")//newName不为空时创建文件夹 { string newPath = Application.StartupPath + "\\Vision\\Calib" + "\\" + newName; // ClsVariable.ProductsPath + "\\" + newName; if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath);//创建文件夹 //MessageBox.Show("料号: " + newName + " 添加成功!"); if (currentName != "" && currentName != string.Empty)//选中某个料号进行复制 { string[] strFileName = Directory.GetFiles(Application.StartupPath + $"\\Vision\\Calib\\{currentName}"); foreach (string file in strFileName) //返回指定目录中文件的名称(包括其路径) { string strSource = Path.GetFileName(file); if (strSource.Contains($"{currentName}")) { string destPath = newPath + "\\" + strSource.Replace($"{currentName}", $"{newName}"); File.Copy(file, destPath, true); } } } } else { MessageBox.Show("料号Calib添加失败,料号已存在!"); } } } } }