Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

498 строки
25 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Newtonsoft.Json.Linq;
  11. using ProductionControl.Utils;
  12. namespace ProductionControl
  13. {
  14. public class Config
  15. {
  16. public static Models.User loginUser;
  17. public static bool HeightDevIOState;
  18. public static Dictionary<string, string> dicDevType = new Dictionary<string, string>();
  19. //
  20. public static string appBasePath = "";
  21. public static string CustomerVer = "A";
  22. //跳过检测设备
  23. public static bool SkipAxis0, SkipAxis1, SkipAxis2, SkipAxis3;
  24. public static bool SkipTension, SkipHeight, SkipLight, SkipScannerGL, SkipScannerCC, SkipSmallAxis;
  25. //启用功能 2023-1023
  26. public static bool OpenMarkErrorStop;
  27. //启用缺陷分布功能
  28. public static bool OpenFlawDistribution;
  29. //启用X轴校准 2023-12-20
  30. public static bool OpenAxisXCalibration;
  31. //DEV
  32. public static int[] Axis_PulseOutMode=new int[4];
  33. public static int[] Axis_MM2PulseNum = new int[4] { 5000, 5000, 5000, 1000};//1mm对应脉冲数
  34. public static int[] Axis_HomeMode = new int[4];
  35. public static int[] Axis_HomeDir = new int[4];//回Home方向
  36. //回HOME速度
  37. public static JArray Axis_HomeVelLow = new JArray();
  38. public static JArray Axis_HomeVelHigh = new JArray();
  39. public static JArray Axis_HomeAcc = new JArray();
  40. public static JArray Axis_HomeDec = new JArray();
  41. public static string IOCard_DeviceNum = "PCI-1730,BID#0";
  42. public static LightDevNameEnum Light_Name= LightDevNameEnum.康仕达;
  43. public static int Light_PortNum = 1;//COM端口号
  44. public static int Tension_PortNum = 7;//COM端口号
  45. public static double Tension_Offset = 0;//张力偏移值
  46. public static string SmallAxis_ComName = "COM2";//COM端口号
  47. public static string HeightDev_IP = "192.168.3.35";
  48. public static int HeightDev_Port = 64000;
  49. public static decimal HeightDev_SafeValue = 0;//厚度传感器安全值(axis移动前的前提条件)
  50. public static string Scanner_GENTL_CTI = "ScannerRuntime\\Win64_x64\\MvFGProducerCXP.cti";//板卡相机
  51. //public static string Scanner_GENTL_CTI = "ScannerRuntime\\Win64_x64\\MvFGProducerCML.cti";//GE相机
  52. public static string SizeEnginePath;
  53. public static string SizeBmp_SavePath, SizeBmp_Compare_SavePath, SizeBmp_Zoom_Image_SavePath;
  54. public static double SizeBmp_SavePath_AutoClear = -1, SizeBmp_Compare_SavePath_AutoClear = -1, SizeBmp_Zoom_Image_SavePath_AutoClear = -1;
  55. //缺陷配置
  56. public static Size Defect_CutSize, Defect_ReSize;
  57. public static float Defect_Thresholds;
  58. public static string Defect_SavePath, Defect_Compress_SavePath, Defect_Small_SavePath, Defect_SrcSmall_SavePath;
  59. public static double Defect_SavePath_AutoClear =-1,Defect_Compress_SavePath_AutoClear=-1,Defect_Small_SavePath_AutoClear=-1, Defect_SrcSmall_SavePath_AutoClear = -1;
  60. public static double Defect_SrcSmall_Thres;
  61. public static bool SaveAllDefectImg, MakeTag;
  62. public static double PT_Offset = 0;//PT偏移值
  63. public static double LineWidth_Offset = 0;//线宽偏移值
  64. //尺寸配置
  65. //public static string Size_LoadPath, Size_FileName;
  66. //
  67. public static Dictionary<CMDName, JObject> CMDProcess;
  68. //DB
  69. //有些服务器防火墙有问题需要加上 min pool size=1 避免认为是恶意请求
  70. //如果用到bulkCopy需要加: AllowLoadLocalInfile=true
  71. //public static string dbMysqlCon = "server=localhost;Database=ProductionDB;Uid=root;Pwd=123456;";
  72. public static string DBConStr = "server=localhost;Database=ProductionDB;Uid=root;Pwd=123456; AllowLoadLocalInfile=true";
  73. //LOG
  74. public static string LogPath;
  75. //WEBService
  76. public static string LocalIp="";
  77. public static int LocalHttpPort=18082, LocalSocketPort;
  78. public static int timeout_http_wait = -1;//webapi超时限制,-1为无限制一直等待
  79. //==========位置 初始位 上料位 下料位(下料位不存在时使用上料位)
  80. public static JObject joPTSetting = new JObject()
  81. {
  82. { "initPT", null },
  83. { "upPT", null},
  84. { "downPT", null}
  85. };
  86. //=========全局按钮
  87. //public static JObject joGButtonSetting = new JObject() {
  88. // { "start", null },
  89. // { "pause", null},
  90. // { "reset", null},
  91. // { "warning", null},
  92. // { "iodefault", null}
  93. //};
  94. public static void DelectPictureFile()
  95. {
  96. //删除文件
  97. Task.Factory.StartNew(() =>
  98. {
  99. // CreateSysFolder();
  100. //
  101. //DeleteFiles(appBasePath + "\\temp\\");
  102. //
  103. if (Defect_SavePath_AutoClear >= 0)
  104. DeleteFiles(Config.Defect_SavePath, Defect_SavePath_AutoClear, true);
  105. if (Defect_Compress_SavePath_AutoClear >= 0)
  106. DeleteFiles(Config.Defect_Compress_SavePath, Defect_Compress_SavePath_AutoClear, true);
  107. if (Defect_Small_SavePath_AutoClear >= 0)
  108. DeleteFiles(Config.Defect_Small_SavePath, Defect_Small_SavePath_AutoClear, true);
  109. if (Defect_SrcSmall_SavePath_AutoClear >= 0)
  110. DeleteFiles(Config.Defect_SrcSmall_SavePath, Defect_SrcSmall_SavePath_AutoClear, true);
  111. //
  112. if (SizeBmp_SavePath_AutoClear >= 0)
  113. DeleteFiles(Config.SizeBmp_SavePath, SizeBmp_SavePath_AutoClear, true);
  114. if (SizeBmp_Compare_SavePath_AutoClear >= 0)
  115. DeleteFiles(Config.SizeBmp_Compare_SavePath, SizeBmp_Compare_SavePath_AutoClear, true);
  116. if (SizeBmp_Zoom_Image_SavePath_AutoClear >= 0)
  117. DeleteFiles(Config.SizeBmp_Zoom_Image_SavePath, SizeBmp_Zoom_Image_SavePath_AutoClear, true);
  118. });
  119. }
  120. public static void LoadAllConfig()
  121. {
  122. if (string.IsNullOrWhiteSpace(appBasePath))
  123. appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  124. //删除文件
  125. Task.Factory.StartNew(() =>
  126. {
  127. CreateSysFolder();
  128. //
  129. DeleteFiles(appBasePath+"\\temp\\");
  130. //
  131. if (Defect_SavePath_AutoClear>=0)
  132. DeleteFiles(Config.Defect_SavePath, Defect_SavePath_AutoClear,true);
  133. if (Defect_Compress_SavePath_AutoClear >= 0)
  134. DeleteFiles(Config.Defect_Compress_SavePath, Defect_Compress_SavePath_AutoClear, true);
  135. if (Defect_Small_SavePath_AutoClear>=0)
  136. DeleteFiles(Config.Defect_Small_SavePath, Defect_Small_SavePath_AutoClear, true);
  137. if (Defect_SrcSmall_SavePath_AutoClear >= 0)
  138. DeleteFiles(Config.Defect_SrcSmall_SavePath, Defect_SrcSmall_SavePath_AutoClear, true);
  139. //
  140. if (SizeBmp_SavePath_AutoClear >= 0)
  141. DeleteFiles(Config.SizeBmp_SavePath, SizeBmp_SavePath_AutoClear, true);
  142. if (SizeBmp_Compare_SavePath_AutoClear >= 0)
  143. DeleteFiles(Config.SizeBmp_Compare_SavePath, SizeBmp_Compare_SavePath_AutoClear, true);
  144. if (SizeBmp_Zoom_Image_SavePath_AutoClear >= 0)
  145. DeleteFiles(Config.SizeBmp_Zoom_Image_SavePath, SizeBmp_Zoom_Image_SavePath_AutoClear, true);
  146. });
  147. InitDevDic();
  148. LoadSysConfig();
  149. LoadPTConfig();
  150. //
  151. string lsCmdPath = appBasePath + "\\CMDProcess\\";
  152. if(!Directory.Exists(lsCmdPath))
  153. Directory.CreateDirectory(lsCmdPath);
  154. string fileName;
  155. CMDProcess = new Dictionary<CMDName, JObject>();
  156. foreach (int item in Enum.GetValues(typeof(CMDName)))//item为key值
  157. {
  158. //string name = Enum.GetName(typeof(CMDName), item);//获取名称
  159. fileName = lsCmdPath + item + ".json";
  160. if (File.Exists(fileName))
  161. CMDProcess.Add((CMDName)item, JObject.Parse(File.ReadAllText(fileName)));
  162. //else
  163. // CMDProcess.Add((CMDName)item, null);
  164. }
  165. }
  166. public static void LoadSysConfig()
  167. {
  168. if (string.IsNullOrWhiteSpace(appBasePath))
  169. appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  170. string configPath = appBasePath + "\\SysConfig.ini";
  171. string lsTmp = "";
  172. CustomerVer = Util.ReadIniValue(configPath, "VER", "CustomerVer");
  173. if (string.IsNullOrWhiteSpace(CustomerVer))
  174. CustomerVer = "A";
  175. //Skip
  176. SkipAxis0 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis0") =="1";
  177. SkipAxis1 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis1") == "1";
  178. SkipAxis2 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis2") == "1";
  179. SkipAxis3 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis3") == "1";
  180. SkipTension = Util.ReadIniValue(configPath, "SKIP", "SkipTension") == "1";
  181. SkipHeight = Util.ReadIniValue(configPath, "SKIP", "SkipHeight") == "1";
  182. SkipLight = Util.ReadIniValue(configPath, "SKIP", "SkipLight") == "1";
  183. SkipScannerGL = Util.ReadIniValue(configPath, "SKIP", "SkipScannerGL") == "1";
  184. SkipScannerCC = Util.ReadIniValue(configPath, "SKIP", "SkipScannerCC") == "1";
  185. SkipSmallAxis = Util.ReadIniValue(configPath, "SKIP", "SkipSmallAxis") == "1";
  186. OpenMarkErrorStop = Util.ReadIniValue(configPath, "SKIP", "MarkErrorStop") == "1";
  187. OpenFlawDistribution = Util.ReadIniValue(configPath, "SKIP", "OpenFlawDistribution") == "1";
  188. OpenAxisXCalibration = Util.ReadIniValue(configPath, "SKIP", "OpenAxisXCalibration") == "1";
  189. //DEV
  190. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_PulseOutMode");
  191. if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[0] = Convert.ToInt32(lsTmp);
  192. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_PulseOutMode");
  193. if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[1] = Convert.ToInt32(lsTmp);
  194. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_PulseOutMode");
  195. if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[2] = Convert.ToInt32(lsTmp);
  196. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_PulseOutMode");
  197. if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[3] = Convert.ToInt32(lsTmp);
  198. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_HomeMode");
  199. if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[0] = Convert.ToInt32(lsTmp);
  200. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_HomeMode");
  201. if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[1] = Convert.ToInt32(lsTmp);
  202. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_HomeMode");
  203. if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[2] = Convert.ToInt32(lsTmp);
  204. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_HomeMode");
  205. if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[3] = Convert.ToInt32(lsTmp);
  206. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_HomeDir");
  207. if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[0] = Convert.ToInt32(lsTmp);
  208. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_HomeDir");
  209. if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[1] = Convert.ToInt32(lsTmp);
  210. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_HomeDir");
  211. if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[2] = Convert.ToInt32(lsTmp);
  212. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_HomeDir");
  213. if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[3] = Convert.ToInt32(lsTmp);
  214. //
  215. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeVelLow");
  216. if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeVelLow = JArray.Parse(lsTmp);
  217. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeVelHigh");
  218. if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeVelHigh = JArray.Parse(lsTmp);
  219. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeAcc");
  220. if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeAcc = JArray.Parse(lsTmp);
  221. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeDec");
  222. if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeDec = JArray.Parse(lsTmp);
  223. //
  224. lsTmp = Util.ReadIniValue(configPath, "DEV", "Light_Name");
  225. if (Util.IsNumber(lsTmp)) Config.Light_Name = (LightDevNameEnum)Convert.ToInt32(lsTmp);
  226. lsTmp = Util.ReadIniValue(configPath, "DEV", "Light_PortNum");
  227. if (Util.IsNumber(lsTmp)) Config.Light_PortNum = Convert.ToInt32(lsTmp);
  228. lsTmp = Util.ReadIniValue(configPath, "DEV", "Tension_PortNum");
  229. if (Util.IsNumber(lsTmp)) Config.Tension_PortNum = Convert.ToInt32(lsTmp);
  230. Config.SmallAxis_ComName = Util.ReadIniValue(configPath, "DEV", "SmallAxis_ComName");
  231. lsTmp = Util.ReadIniValue(configPath, "DEV", "Tension_Offset");
  232. if (string.IsNullOrEmpty(lsTmp))
  233. Config.Tension_Offset = 0;
  234. else
  235. Config.Tension_Offset = double.Parse(lsTmp);
  236. HeightDev_IP = Util.ReadIniValue(configPath, "DEV", "HeightDev_IP");
  237. lsTmp = Util.ReadIniValue(configPath, "DEV", "HeightDev_Port");
  238. if (Util.IsNumber(lsTmp)) Config.HeightDev_Port = Convert.ToInt32(lsTmp);
  239. lsTmp = Util.ReadIniValue(configPath, "DEV", "HeightDev_SafeValue");
  240. if (Util.IsNumber(lsTmp)) Config.HeightDev_SafeValue = Convert.ToDecimal(lsTmp);
  241. IOCard_DeviceNum = Util.ReadIniValue(configPath, "DEV", "IOCard_DeviceNum");
  242. Defect_SavePath = Util.ReadIniValue(configPath, "DEV", "Defect_SavePath");
  243. lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_SavePath_AutoClear");
  244. if (Util.IsDecimal(lsTmp)) Defect_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  245. Defect_Compress_SavePath = Util.ReadIniValue(configPath, "DEV", "Defect_Compress_SavePath");
  246. lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_Compress_SavePath_AutoClear");
  247. if (Util.IsDecimal(lsTmp)) Defect_Compress_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  248. Defect_Small_SavePath = Util.ReadIniValue(configPath, "DEV", "Defect_Small_SavePath");
  249. lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_Small_SavePath_AutoClear");
  250. if (Util.IsDecimal(lsTmp)) Defect_Small_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  251. lsTmp = Util.ReadIniValue(configPath, "DEV", "SaveAllDefectImg");
  252. SaveAllDefectImg = (lsTmp == "1");
  253. MakeTag = Util.ReadIniValue(configPath, "DEV", "MakeTag") == "1";
  254. SizeEnginePath = Util.ReadIniValue(configPath, "DEV", "SizeEnginePath");
  255. //////////////////////////////////////////////////////////////
  256. ///原始缺陷小图
  257. Defect_SrcSmall_SavePath = Util.ReadIniValue(configPath, "DEV", "Defect_SrcSmall_SavePath");
  258. lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_SrcSmall_SavePath_AutoClear");
  259. if (Util.IsDecimal(lsTmp)) Defect_SrcSmall_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  260. lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_SrcSmall_Thres");
  261. if (Util.IsDecimal(lsTmp)) Defect_SrcSmall_Thres = Convert.ToDouble(lsTmp);
  262. //////////////////////////////////////////////////////////////
  263. SizeBmp_SavePath = Util.ReadIniValue(configPath, "DEV", "SizeBmp_SavePath");
  264. lsTmp = Util.ReadIniValue(configPath, "DEV", "SizeBmp_SavePath_AutoClear");
  265. if (Util.IsDecimal(lsTmp)) SizeBmp_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  266. SizeBmp_Compare_SavePath = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Compare_SavePath");
  267. lsTmp = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Compare_SavePath_AutoClear");
  268. if (Util.IsDecimal(lsTmp)) SizeBmp_Compare_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  269. SizeBmp_Zoom_Image_SavePath = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Zoom_Image_SavePath");
  270. lsTmp = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Zoom_Image_SavePath_AutoClear");
  271. if (Util.IsDecimal(lsTmp)) SizeBmp_Zoom_Image_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  272. lsTmp = Util.ReadIniValue(configPath, "DEV", "PT_Offset");
  273. if (string.IsNullOrEmpty(lsTmp))
  274. Config.PT_Offset = 0;
  275. else
  276. Config.PT_Offset = double.Parse(lsTmp);
  277. lsTmp = Util.ReadIniValue(configPath, "DEV", "LineWidth_Offset");
  278. if (string.IsNullOrEmpty(lsTmp))
  279. Config.LineWidth_Offset = 0;
  280. else
  281. Config.LineWidth_Offset = double.Parse(lsTmp);
  282. //DB
  283. DBConStr = Util.ReadIniValue(configPath, "DB", "DBConStr");
  284. //HTTP
  285. LocalIp= Util.ReadIniValue(configPath, "HTTP", "LocalIp");
  286. lsTmp = Util.ReadIniValue(configPath, "HTTP", "LocalHttpPort");
  287. if (Util.IsNumber(lsTmp)) Config.LocalHttpPort = Convert.ToInt32(lsTmp);
  288. lsTmp = Util.ReadIniValue(configPath, "HTTP", "LocalSocketPort");
  289. if (Util.IsNumber(lsTmp)) Config.LocalSocketPort = Convert.ToInt32(lsTmp);
  290. //LOG
  291. LogPath = Util.ReadIniValue(configPath, "LOG", "LogPath");
  292. //缺陷配置
  293. Defect_CutSize = new Size(592, 532);
  294. Defect_ReSize = new Size(224, 224);
  295. Defect_Thresholds = 0.4f;
  296. }
  297. public static void LoadPTConfig()
  298. {
  299. if (string.IsNullOrWhiteSpace(appBasePath))
  300. appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  301. string configPath = appBasePath + "\\PTConfig.json";
  302. string lsTmp = File.ReadAllText(configPath);
  303. joPTSetting = JObject.Parse(lsTmp);
  304. }
  305. private static void InitDevDic()
  306. {
  307. //dicDevType.Add("CodeScanner", "扫码枪");//固定为必有开始
  308. if(!dicDevType.ContainsKey("IOCard")) dicDevType.Add("IOCard", "I/O控制");
  309. if (!dicDevType.ContainsKey("Tension")) dicDevType.Add("Tension", "张力测量");
  310. if (!dicDevType.ContainsKey("Height")) dicDevType.Add("Height", "厚度测量");
  311. if (!dicDevType.ContainsKey("Axis")) dicDevType.Add("Axis", "滑台电机");
  312. if (!dicDevType.ContainsKey("AxisTag")) dicDevType.Add("AxisTag", "滑台&测量");
  313. if (!dicDevType.ContainsKey("Light")) dicDevType.Add("Light", "光源");
  314. if (!dicDevType.ContainsKey("Scanner_GENTL")) dicDevType.Add("Scanner_GENTL", "相机GENTL");
  315. if (!dicDevType.ContainsKey("Scanner_CC")) dicDevType.Add("Scanner_CC", "相机CC");
  316. if (!dicDevType.ContainsKey("SmallAxis")) dicDevType.Add("SmallAxis", "镜头电机");
  317. if (!dicDevType.ContainsKey("Defect")) dicDevType.Add("Defect", "缺陷检测");
  318. if (!dicDevType.ContainsKey("Size")) dicDevType.Add("Size", "尺寸测量");
  319. if (!dicDevType.ContainsKey("For")) dicDevType.Add("For", "For循环");
  320. if (!dicDevType.ContainsKey("If")) dicDevType.Add("If", "If条件");
  321. }
  322. private static void CreateSysFolder()
  323. {
  324. appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  325. string dirPath = appBasePath + "\\Attachment\\";
  326. if(!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
  327. dirPath = appBasePath + "\\Temp\\";
  328. if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
  329. }
  330. /// <summary>
  331. ///
  332. /// </summary>
  333. /// <param name="dirPath"></param>
  334. /// <param name="days"></param>
  335. /// <param name="delDir">把日期文件夹也按days进行删除,格式:yyyyMMdd</param>
  336. private static void DeleteFiles(string dirPath, double days = 0, bool delDir = false)
  337. {
  338. if (!Directory.Exists(dirPath)) return;
  339. DateTime delDate = DateTime.Now.AddDays(-days);
  340. API.OutputDebugString($"清除{delDate.ToString("yyyyMMdd")}前记录:{dirPath}。");
  341. //删除dirPath中的过期文件
  342. string[] files_list = Directory.GetFiles(dirPath);
  343. if (days > 0)
  344. files_list = files_list.Select(file => new FileInfo(file))
  345. .Where(f => f.CreationTime <= delDate)
  346. .Select(x => x.FullName)
  347. .ToArray();
  348. foreach (string file in files_list)
  349. {
  350. try
  351. {
  352. API.OutputDebugString($"清除文件:{file}...");
  353. File.Delete(file);
  354. }
  355. catch (Exception ex)
  356. {
  357. API.OutputDebugString($"Ex1:{ex.Message}");
  358. }
  359. }
  360. string[] dirsList = Directory.GetDirectories(dirPath, "20*");
  361. foreach (string dir in dirsList)
  362. {
  363. string[] file_list = Directory.GetFiles(dir);
  364. if (days > 0)
  365. file_list = file_list.Select(file => new FileInfo(file))
  366. .Where(f => f.CreationTime <= delDate)
  367. .Select(x => x.FullName)
  368. .ToArray();
  369. foreach (string file in file_list)
  370. {
  371. try {
  372. API.OutputDebugString($"清除文件:{file}...");
  373. File.Delete(file);
  374. } catch (Exception ex) {
  375. API.OutputDebugString($"Ex1:{ex.Message}");
  376. }
  377. }
  378. }
  379. //
  380. if (delDir)
  381. {
  382. int dateNum = 0;
  383. if (days > 1)
  384. dateNum = Convert.ToInt32(delDate.ToString("yyyyMMdd"));
  385. else if (days > 0)
  386. dateNum = Convert.ToInt32(delDate.ToString("yyyyMMdd")) - 1;
  387. string floderName;
  388. string[] dirList = Directory.GetDirectories(dirPath,"20*");
  389. foreach (string dir in dirList)
  390. {
  391. floderName= dir.Trim('\\');
  392. floderName = floderName.Substring(floderName.LastIndexOf('\\') + 1);
  393. if (floderName.Length == 8 && Util.IsNumber(floderName) && Convert.ToInt32(floderName) <= dateNum)
  394. {
  395. try
  396. {
  397. API.OutputDebugString($"清除目录:{dir}...");
  398. Directory.Delete(dir,true);
  399. }
  400. catch (Exception ex)
  401. {
  402. API.OutputDebugString($"Ex2:{ex.Message}");
  403. }
  404. }
  405. }
  406. }
  407. }
  408. public static long GetHardDiskSpace(string str_HardDiskName)
  409. {
  410. long result = 0L;
  411. try
  412. {
  413. str_HardDiskName += ":\\";
  414. DriveInfo[] drives = DriveInfo.GetDrives();
  415. DriveInfo[] array = drives;
  416. foreach (DriveInfo driveInfo in array)
  417. {
  418. if (driveInfo.Name == str_HardDiskName)
  419. {
  420. result = driveInfo.TotalFreeSpace / 1073741824;
  421. }
  422. }
  423. }
  424. catch {
  425. result = 0;
  426. }
  427. return result;
  428. }
  429. public static bool CheckDisk(int max = 10)
  430. {
  431. string path = Config.Defect_SavePath;
  432. string volume = path.Substring(0, path.IndexOf(':'));
  433. long freespace = GetHardDiskSpace(volume);
  434. if (freespace < max)
  435. {
  436. string tip = $"当前{volume}硬盘容量:{freespace}GB,小于{max}GB。注意清理!!";
  437. MessageBox.Show(tip, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  438. return false;
  439. }
  440. return true;
  441. }
  442. }
  443. }