123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- 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添加失败,料号已存在!");
- }
- }
- }
- }
- }
|