Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

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