FrmAddMaterial.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11. using System.IO;
  12. using BaseLibRWFile;
  13. namespace HRT_Measure
  14. {
  15. public partial class FrmAddMaterial : Form
  16. {
  17. public FrmAddMaterial()
  18. {
  19. InitializeComponent();
  20. }
  21. #region 控件
  22. private void FrmAddMaterial_Load(object sender, EventArgs e)
  23. {
  24. //this.Size = FrmMain.sizeProject;
  25. splitContainer1.SplitterDistance = this.Width * 2 / 3;
  26. //iniAddMaterial.filePath = ClsVariable.defaultIniPath;
  27. //VisionHelp.Instance.ProConfig.StrCurrentPartNumber;//strSelectedValue = iniAddMaterial.ReadIni("CurrentProduct", "ProductName");
  28. //asc.controllInitializeSize(this);
  29. CheckListBoxUpdate();
  30. }
  31. private void btnUpdate_Click(object sender, EventArgs e)//更新
  32. {
  33. CheckListBoxUpdate();
  34. }
  35. private void btnSeleted_Click(object sender, EventArgs e)//选中
  36. {
  37. if (MessageBox.Show("请确定是否更换料号?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  38. {
  39. if(clbSeletedItem.SelectedItem.ToString() != ProgramHelp.Instance.ProConfig.当前料号)
  40. {
  41. //iniAddMaterial.filePath = ClsVariable.defaultIniPath;
  42. //iniAddMaterial.WriteIni("CurrentProduct", "ProductName", strSelectedValue);
  43. ProgramHelp.Instance.ProConfig.当前料号 = clbSeletedItem.SelectedItem.ToString();
  44. lblCurrentProduct.Text = "当前料号为:" + ProgramHelp.Instance.ProConfig.当前料号;
  45. //加载相机等内容
  46. //try
  47. //{
  48. // CogCameraInitialize.Instance.CameraClose();
  49. //}
  50. //catch (Exception ex)
  51. //{
  52. // MessageBox.Show(ex.ToString());
  53. //}
  54. //try
  55. //{
  56. // CogCameraInitialize.Instance.Initialize();
  57. //}
  58. //catch (Exception ex)
  59. //{
  60. // MessageBox.Show(ex.ToString());
  61. //}
  62. ProgramHelp.Instance.frmMain.ChangePartNumber(ProgramHelp.Instance.ProConfig.当前料号);
  63. }
  64. else
  65. {
  66. MessageBox.Show("更换料号不能为同一料号!!!");
  67. }
  68. }
  69. }
  70. private void btnDelete_Click(object sender, EventArgs e)//删除
  71. {
  72. if (MessageBox.Show("请确定是否删除料号?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  73. {
  74. //iniAddMaterial.filePath = ClsVariable.defaultIniPath;
  75. if (clbSeletedItem.SelectedItem.ToString() != ProgramHelp.Instance.ProConfig.当前料号)//clsIniFile.IniReadValue("DefaultProduct", "ProductName") strSelectedValue != iniAddMaterial.ReadIni("CurrentProduct", "ProductName")
  76. {
  77. if (clbSeletedItem.SelectedItems.Count > 0)
  78. {
  79. //string strName = clbSeletedItem.SelectedItem.ToString();//获取当前选中项
  80. //string strPath = ClsVariable.ProductsPath + "\\" + strName;
  81. FileOperate.DeleteFolder(Application.StartupPath + $"\\Config\\{clbSeletedItem.SelectedItem.ToString()}");
  82. CheckListBoxUpdate();
  83. MessageBox.Show("删除成功!!!");
  84. }
  85. }
  86. else
  87. {
  88. MessageBox.Show("当前料号不允许删除!!!");
  89. }
  90. }
  91. }
  92. private void btnAdd_Click(object sender, EventArgs e)//新增
  93. {
  94. AddNewMaterial(ProgramHelp.Instance.ProConfig.当前料号, txtNewName.Text.Trim());
  95. }
  96. private void btnReName_Click(object sender, EventArgs e)//重命名
  97. {
  98. if (clbSeletedItem.SelectedIndex >= 0 && txtNewName.Text.Trim().Length > 0)
  99. ReName(clbSeletedItem.Items[clbSeletedItem.SelectedIndex].ToString(), txtNewName.Text.Trim());
  100. else
  101. MessageBox.Show("请选择操作项或输入名称!");
  102. }
  103. private void timer1_Tick(object sender, EventArgs e)//定时刷新
  104. {
  105. //iniAddMaterial.filePath = ClsVariable.defaultIniPath;
  106. //lblCurrentProduct.Text = "当前料号为:" + iniAddMaterial.ReadIni("CurrentProduct", "ProductName");//clsIniFile.IniReadValue("DefaultProduct", "ProductName") /
  107. //ClsVariable.currentIniPath = iniAddMaterial.ReadIni("CurrentProduct", "ProductName");//更新当前料号
  108. }
  109. #endregion
  110. #region 方法
  111. public void CheckListBoxUpdate()//更新checkedListBox
  112. {
  113. clbSeletedItem.Items.Clear();
  114. DirectoryInfo di = new DirectoryInfo(Application.StartupPath + "\\Config");//ClsVariable.ProductsPath
  115. DirectoryInfo[] subDirectories = di.GetDirectories();//返回当前目录的子目录
  116. foreach (DirectoryInfo dir in subDirectories)
  117. {
  118. clbSeletedItem.Items.Add(dir.Name.ToString());//显示当前所有料号文件夹到checkedListbox里
  119. }
  120. if (Directory.Exists(Application.StartupPath + $"\\Config\\{ProgramHelp.Instance.ProConfig.当前料号}"))//选中当前料号 ClsVariable.defaultIniPath
  121. {
  122. //iniAddMaterial.filePath = ClsVariable.defaultIniPath;
  123. //string currentName = iniAddMaterial.ReadIni("CurrentProduct", "ProductName");
  124. bool setTag = false;
  125. for (int i = 0; i < clbSeletedItem.Items.Count; i++)
  126. {
  127. if (clbSeletedItem.Items[i].ToString() == ProgramHelp.Instance.ProConfig.当前料号)
  128. {
  129. clbSeletedItem.SetItemCheckState(i, CheckState.Checked);//设置指定索引处项的复选状态
  130. setTag = true;
  131. lblCurrentProduct.Text = "当前料号为:" + ProgramHelp.Instance.ProConfig.当前料号;
  132. //ClsVariable.currentIniPath = iniAddMaterial.ReadIni("CurrentProduct", "ProductName");//更新当前料号
  133. return;
  134. }
  135. }
  136. if (setTag == false)
  137. {
  138. //暂时不考虑
  139. //clsIniFile.IniWriteValue("DefaultProduct", "ProductName", "");
  140. }
  141. }
  142. }
  143. private void clbSeletedItem_ItemCheck(object sender, ItemCheckEventArgs e)//更改选中状态
  144. {
  145. for (int i = 0; i < clbSeletedItem.Items.Count; i++)
  146. {
  147. if (i != e.Index)
  148. {
  149. clbSeletedItem.SetItemCheckState(i, CheckState.Unchecked); //设置指定索引处项的复选状态
  150. }
  151. }
  152. //strSelectedValue = clbSeletedItem.Items[e.Index].ToString().Trim();
  153. }
  154. public void AddNewMaterial(string currentName, string newName)//添加新料号
  155. {
  156. try
  157. {
  158. if (newName != "")//newName不为空时创建文件夹
  159. {
  160. string newPath = Application.StartupPath + "\\Config" + "\\" + newName; // ClsVariable.ProductsPath + "\\" + newName;
  161. if (!Directory.Exists(newPath))
  162. {
  163. Directory.CreateDirectory(newPath);//创建文件夹
  164. MessageBox.Show("料号: " + newName + " 添加成功!");
  165. if (currentName != "" && currentName != string.Empty)//选中某个料号进行复制
  166. {
  167. string [] strFileName = Directory.GetFiles(Application.StartupPath + $"\\Config\\{currentName}");
  168. foreach (string file in strFileName) //返回指定目录中文件的名称(包括其路径)
  169. {
  170. string strSource = Path.GetFileName(file);
  171. if (strSource.Contains($"{currentName}"))
  172. {
  173. string destPath = newPath + "\\" + strSource.Replace($"{currentName}", $"{newName}");
  174. File.Copy(file, destPath, true);
  175. }
  176. }
  177. }
  178. }
  179. else
  180. {
  181. MessageBox.Show("料号添加失败,料号已存在!");
  182. }
  183. //CopyConfigToNewFoleder(currentName, newName);
  184. //CopyCalToNewFoleder(currentName, newName);
  185. CheckListBoxUpdate();
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. MessageBox.Show(ex.ToString());
  191. }
  192. }
  193. public void ReName(string selectName, string newName)//重命名
  194. {
  195. try
  196. {
  197. if (selectName != "" && selectName != string.Empty)
  198. {
  199. if (selectName == ProgramHelp.Instance.ProConfig.当前料号)//clsIniFile.IniReadValue("DefaultProduct", "ProductName")
  200. {
  201. MessageBox.Show("当前料号不能重命名!");
  202. }
  203. else
  204. {
  205. if (Directory.Exists(Application.StartupPath + "\\Config" + $"\\{selectName}"))
  206. {
  207. if (newName != "" && newName != string.Empty)
  208. {
  209. Directory.CreateDirectory(Application.StartupPath + "\\Config" + "\\" + newName);//新建文件夹
  210. foreach (string file in Directory.GetFiles(Application.StartupPath + $"\\Config\\{selectName}")) //返回指定目录中文件的名称(包括其路径)
  211. {
  212. if (Path.GetFileName(file).Contains($"{selectName}"))
  213. {
  214. string destPath = Path.GetFileName(file).Replace($"{selectName}", $"{newName}");
  215. File.Move(Path.GetFileName(file), destPath);
  216. }
  217. }
  218. FileOperate.DeleteFolder(Application.StartupPath + "\\Config" + $"\\{selectName}");
  219. }
  220. }
  221. }
  222. CheckListBoxUpdate();
  223. }
  224. }
  225. catch (Exception ex)
  226. {
  227. MessageBox.Show(ex.ToString());
  228. }
  229. }
  230. #endregion
  231. public void CopyConfigToNewFoleder(string currentName, string newName)
  232. {
  233. if (newName != "")//newName不为空时创建文件夹
  234. {
  235. string newPath = Application.StartupPath + "\\Config" + "\\" + newName; // ClsVariable.ProductsPath + "\\" + newName;
  236. if (!Directory.Exists(newPath))
  237. {
  238. Directory.CreateDirectory(newPath);//创建文件夹
  239. //MessageBox.Show("料号: " + newName + " 添加成功!");
  240. if (currentName != "" && currentName != string.Empty)//选中某个料号进行复制
  241. {
  242. string[] strFileName = Directory.GetFiles(Application.StartupPath + $"\\Config\\{currentName}");
  243. foreach (string file in strFileName) //返回指定目录中文件的名称(包括其路径)
  244. {
  245. string strSource = Path.GetFileName(file);
  246. if (strSource.Contains($"{currentName}"))
  247. {
  248. string destPath = newPath + "\\" + strSource.Replace($"{currentName}", $"{newName}");
  249. File.Copy(file, destPath, true);
  250. }
  251. }
  252. }
  253. }
  254. else
  255. {
  256. MessageBox.Show("料号添加失败,料号已存在!");
  257. }
  258. }
  259. }
  260. public void CopyCalToNewFoleder(string currentName, string newName)
  261. {
  262. if (newName != "")//newName不为空时创建文件夹
  263. {
  264. string newPath = Application.StartupPath + "\\Vision\\Calib" + "\\" + newName; // ClsVariable.ProductsPath + "\\" + newName;
  265. if (!Directory.Exists(newPath))
  266. {
  267. Directory.CreateDirectory(newPath);//创建文件夹
  268. //MessageBox.Show("料号: " + newName + " 添加成功!");
  269. if (currentName != "" && currentName != string.Empty)//选中某个料号进行复制
  270. {
  271. string[] strFileName = Directory.GetFiles(Application.StartupPath + $"\\Vision\\Calib\\{currentName}");
  272. foreach (string file in strFileName) //返回指定目录中文件的名称(包括其路径)
  273. {
  274. string strSource = Path.GetFileName(file);
  275. if (strSource.Contains($"{currentName}"))
  276. {
  277. string destPath = newPath + "\\" + strSource.Replace($"{currentName}", $"{newName}");
  278. File.Copy(file, destPath, true);
  279. }
  280. }
  281. }
  282. }
  283. else
  284. {
  285. MessageBox.Show("料号Calib添加失败,料号已存在!");
  286. }
  287. }
  288. }
  289. }
  290. }