BaseUtil.cs 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804
  1. using LabelManager2;
  2. using Microsoft.Win32;
  3. using NPOI.SS.Formula.Functions;
  4. using Seagull.BarTender.Print;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Net;
  15. using System.Net.Sockets;
  16. using System.Reflection;
  17. using System.Text;
  18. using System.Text.RegularExpressions;
  19. using System.Threading;
  20. using System.Windows.Forms;
  21. using System.Windows.Forms.DataVisualization.Charting;
  22. using System.Xml;
  23. using UAS_MES_NEW.CustomControl.DataGrid_View;
  24. using UAS_MES_NEW.CustomControl.GroupBoxWithBorder;
  25. using UAS_MES_NEW.CustomControl.TextBoxWithIcon;
  26. using UAS_MES_NEW.CustomControl.ValueLabel;
  27. using UAS_MES_NEW.Entity;
  28. using static System.Windows.Forms.Control;
  29. namespace UAS_MES_NEW.PublicMethod
  30. {
  31. class BaseUtil
  32. {
  33. public static string DToAny(double DB, int Type, string char_out)
  34. {
  35. string H = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  36. long D;
  37. double B;
  38. string tempD = "", tempB = "";
  39. for (int i = 0; i < char_out.Length; i++)
  40. {
  41. H = H.Replace(char_out[i].ToString(), "");
  42. }
  43. if (Type != H.Length && Type != 16 & Type != 10)
  44. {
  45. ShowError("字符长度截取错误,请检查进制和去除字符");
  46. return "";
  47. }
  48. D = (long)DB;
  49. B = DB - D;
  50. if (D == 0)
  51. {
  52. tempD = "0";
  53. }
  54. while (D != 0)
  55. {
  56. tempD = H[(((int)D % Type))] + tempD;
  57. D = D / Type;
  58. }
  59. for (int i = 0; i < 7; i++)
  60. {
  61. if (B == 0)
  62. {
  63. break;
  64. }
  65. tempB += H[((int)(B * Type))];
  66. B = B * Type - (int)(B * Type);
  67. }
  68. if (tempB == "")
  69. {
  70. return tempD;
  71. }
  72. else
  73. {
  74. return tempD + "." + tempB;
  75. }
  76. }
  77. /// <summary>
  78. /// 检测TCP连接是否存在还是已经中断
  79. /// </summary>
  80. /// <param name="c"></param>
  81. /// <returns></returns>
  82. public static bool IsOnline(TcpClient c)
  83. {
  84. return !((c.Client.Poll(1000, SelectMode.SelectRead) && (c.Client.Available == 0)) || !c.Client.Connected);
  85. }
  86. public static Control GetControl(Control ctl, string Name)
  87. {
  88. if (ctl.Name == Name)
  89. return ctl;
  90. else
  91. return GetControl(ctl.Parent, Name);
  92. }
  93. public static string DToAny(double DB, int Type)
  94. {
  95. string H = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  96. string H1 = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
  97. string H2 = "0123456789BCDFGHJKLMNPQRSTVWXYZ";
  98. long D;
  99. double B;
  100. string tempD = "", tempB = "";
  101. if (Type == 32)
  102. {
  103. return GetRandomChar(H1);
  104. }
  105. else if (Type == 31)
  106. {
  107. D = (long)DB;
  108. B = DB - D;
  109. if (D == 0)
  110. {
  111. tempD = "0";
  112. }
  113. while (D != 0)
  114. {
  115. tempD = H2[(((int)D % Type))] + tempD;
  116. D = D / Type;
  117. }
  118. for (int i = 0; i < 7; i++)
  119. {
  120. if (B == 0)
  121. {
  122. break;
  123. }
  124. tempB += H2[((int)(B * Type))];
  125. B = B * Type - (int)(B * Type);
  126. }
  127. if (tempB == "")
  128. {
  129. return tempD;
  130. }
  131. else
  132. {
  133. return tempD + "." + tempB;
  134. }
  135. }
  136. else
  137. {
  138. D = (long)DB;
  139. B = DB - D;
  140. if (D == 0)
  141. {
  142. tempD = "0";
  143. }
  144. while (D != 0)
  145. {
  146. tempD = H[(((int)D % Type))] + tempD;
  147. D = D / Type;
  148. }
  149. for (int i = 0; i < 7; i++)
  150. {
  151. if (B == 0)
  152. {
  153. break;
  154. }
  155. tempB += H[((int)(B * Type))];
  156. B = B * Type - (int)(B * Type);
  157. }
  158. if (tempB == "")
  159. {
  160. return tempD;
  161. }
  162. else
  163. {
  164. return tempD + "." + tempB;
  165. }
  166. }
  167. }
  168. public static string GetRandomChar(string str)
  169. {
  170. byte[] buffer = Guid.NewGuid().ToByteArray();
  171. int iSeed = BitConverter.ToInt32(buffer, 0);
  172. Random random = new Random(iSeed);
  173. string data = "";
  174. for (int i = 0; i < 5; i++)
  175. {
  176. int index = random.Next(0, 31);
  177. data += str[index];
  178. }
  179. return data;
  180. }
  181. /// <summary>
  182. /// 通过DataTable的ColumnName和Caption来拼接一条语句
  183. /// </summary>
  184. /// <param name=""></param>
  185. /// <returns></returns>
  186. public static string GetGridViewSelectContent(DataGridView d)
  187. {
  188. StringBuilder selectConetnt = new StringBuilder();
  189. DataTable dt = (DataTable)d.DataSource;
  190. if (dt == null)
  191. {
  192. foreach (DataGridViewColumn dc in d.Columns)
  193. {
  194. if (dc.DataPropertyName != "" && dc.DataPropertyName != null)
  195. {
  196. selectConetnt.Append(dc.Name + " as " + dc.Name + ",");
  197. }
  198. }
  199. }
  200. else
  201. {
  202. foreach (DataColumn dc in dt.Columns)
  203. {
  204. selectConetnt.Append(dc.Caption + " as " + dc.ColumnName + ",");
  205. }
  206. }
  207. return selectConetnt.Remove(selectConetnt.Length - 1, 1).ToString();
  208. }
  209. /// <summary>
  210. /// 禁止DataGirdView排序
  211. /// </summary>
  212. /// <param name="Dgv"></param>
  213. public static void DataGridViewNotSort(DataGridView Dgv)
  214. {
  215. foreach (DataGridViewColumn item in Dgv.Columns)
  216. item.SortMode = DataGridViewColumnSortMode.NotSortable;
  217. }
  218. public static string GetLocalIP()
  219. {
  220. try
  221. {
  222. string HostName = Dns.GetHostName(); //得到主机名
  223. IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
  224. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  225. {
  226. //从IP地址列表中筛选出IPv4类型的IP地址
  227. //AddressFamily.InterNetwork表示此IP为IPv4,
  228. //AddressFamily.InterNetworkV6表示此地址为IPv6类型
  229. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  230. {
  231. return IpEntry.AddressList[i].ToString();
  232. }
  233. }
  234. return "";
  235. }
  236. catch (Exception ex)
  237. {
  238. MessageBox.Show("获取本机IP出错:" + ex.Message);
  239. return "";
  240. }
  241. }
  242. /// <summary>
  243. /// 通过字段和其展示的中文值获取查询的内容
  244. /// </summary>
  245. /// <param name="field"></param>
  246. /// <param name="cnfield"></param>
  247. /// <returns></returns>
  248. public static string GetSelectContentByStringArray(string[] field, string[] cnfield)
  249. {
  250. StringBuilder sb = new StringBuilder();
  251. for (int i = 0; i < field.Length; i++)
  252. {
  253. sb.Append(field[i] + " as " + cnfield[i] + ",");
  254. }
  255. //去掉多余的逗号
  256. sb.Remove(sb.Length - 1, 1);
  257. return sb.ToString();
  258. }
  259. static int SortByIndex(Control A, Control B)
  260. {
  261. return A.TabIndex.CompareTo(B.TabIndex);
  262. }
  263. public static bool CheckBarTenderEnable()
  264. {
  265. var reg = new string[] {
  266. @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
  267. @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
  268. };
  269. string tempType = null;
  270. int softNum = 0;//所有已经安装的程序数量
  271. RegistryKey currentKey = null;
  272. var ls = new List<Dictionary<string, string>>();
  273. foreach (var item222 in reg)
  274. {
  275. object displayName = null, uninstallString = null, installLocation = null, releaseType = null;
  276. RegistryKey pregkey = Registry.LocalMachine.OpenSubKey(item222);//获取指定路径下的键
  277. foreach (string item in pregkey.GetSubKeyNames()) //循环所有子键
  278. {
  279. currentKey = pregkey.OpenSubKey(item);
  280. displayName = currentKey.GetValue("DisplayName"); //获取显示名称
  281. installLocation = currentKey.GetValue("InstallLocation"); //获取安装路径
  282. uninstallString = currentKey.GetValue("UninstallString"); //获取卸载字符串路径
  283. releaseType = currentKey.GetValue("ReleaseType"); //发行类型,值是Security Update为安全更新,Update为更新
  284. bool isSecurityUpdate = false;
  285. if (releaseType != null)
  286. {
  287. tempType = releaseType.ToString();
  288. if (tempType == "Security Update" || tempType == "Update")
  289. {
  290. isSecurityUpdate = true;
  291. }
  292. }
  293. if (!isSecurityUpdate && displayName != null && uninstallString != null)
  294. {
  295. softNum++;
  296. if (displayName.ToString() == "BarTender 10.1")
  297. {
  298. return true;
  299. }
  300. }
  301. }
  302. }
  303. return false;
  304. }
  305. /// <summary>
  306. /// 传入控件的集合和DataTable,通过判断控件的名称和数据源的列的描述来匹配,支持单层的GroupBox和Panel
  307. /// </summary>
  308. /// <param name="collection"></param>
  309. /// <param name="dt"></param>
  310. public static void SetFormValue(ControlCollection collection, DataTable dt)
  311. {
  312. List<Control> ctl = new List<Control>();
  313. //将控件按照索引排序
  314. for (int i = 0; i < collection.Count; i++)
  315. {
  316. for (int j = 0; j < ctl.Count; j++)
  317. {
  318. if (!ctl.Contains(collection[i]))
  319. {
  320. if (collection[i].TabIndex > ctl[j].TabIndex)
  321. {
  322. ctl.Add(collection[i]);
  323. break;
  324. }
  325. else
  326. {
  327. ctl.Insert(j, collection[i]);
  328. break;
  329. }
  330. }
  331. }
  332. if (ctl.Count == 0)
  333. {
  334. ctl.Add(collection[i]);
  335. }
  336. }
  337. ctl.Sort(SortByIndex);
  338. //DataTable存在数据才进行赋值操作
  339. if (dt.Rows.Count > 0)
  340. {
  341. for (int i = 0; i < ctl.Count; i++)
  342. {
  343. //如果含有子控件则进行递归调用
  344. if (ctl[i].Controls.Count > 0)
  345. SetFormValue(ctl[i].Controls, dt);
  346. string controlName = ctl[i].Name;
  347. string controlsTag = ctl[i].Tag == null ? "" : ctl[i].Tag.ToString();
  348. //默认给TextBox和Label赋值
  349. if (ctl[i] is TextBox || ctl[i] is Label || ctl[i] is SearchTextBox || ctl[i] is MaCodeSearchTextBox || ctl[i] is EnterTextBox || ctl[i] is TextBoxGeneratePaCode || ctl[i] is NumericUpDown || ctl[i] is TextBoxWithTextArea || ctl[i] is RichTextBox)
  350. {
  351. for (int j = 0; j < dt.Columns.Count; j++)
  352. {
  353. if (controlName.ToUpper() == dt.Columns[j].Caption.ToUpper() || controlsTag.ToUpper() == dt.Columns[j].Caption.ToUpper())
  354. {
  355. //字段含有Status内容的才进行转换
  356. if (controlName.ToUpper().Contains("STATUS") || controlsTag.ToUpper().Contains("STATUS"))
  357. {
  358. //对审批状态进行判断
  359. switch (dt.Rows[0][j].ToString().ToUpper())
  360. {
  361. case "ENTERING":
  362. ctl[i].Text = "在录入";
  363. break;
  364. case "UNAPPROVED":
  365. ctl[i].Text = "未批准";
  366. break;
  367. case "COMMITED":
  368. ctl[i].Text = "已提交";
  369. break;
  370. case "APPROVE":
  371. ctl[i].Text = "已批准";
  372. break;
  373. case "AUDITED":
  374. ctl[i].Text = "已审核";
  375. break;
  376. case "STARTED":
  377. ctl[i].Text = "已下放";
  378. break;
  379. case "UNCHECK":
  380. ctl[i].Text = "待检验";
  381. break;
  382. case "CHECKING":
  383. ctl[i].Text = "检验中";
  384. break;
  385. default:
  386. ctl[i].Text = dt.Rows[0][j].ToString();
  387. break;
  388. }
  389. }
  390. else
  391. ctl[i].Text = dt.Rows[0][j].ToString();
  392. }
  393. }
  394. }
  395. //对封装在GroupBox的和Panel的控件进行批量赋值
  396. if (ctl[i] is GroupBox || ctl[i] is Panel || ctl[i] is GroupBoxWithBorder)
  397. {
  398. for (int j = 0; j < ctl[i].Controls.Count; j++)
  399. {
  400. controlName = ctl[i].Controls[j].Name;
  401. if (ctl[i].Controls[j] is TextBox || ctl[i].Controls[j] is Label || ctl[i].Controls[j] is SearchTextBox || ctl[i].Controls[j] is MaCodeSearchTextBox)
  402. {
  403. for (int k = 0; k < dt.Columns.Count; k++)
  404. {
  405. if (controlName.ToUpper() == dt.Columns[k].Caption.ToUpper())
  406. {
  407. ctl[i].Controls[j].Text = dt.Rows[0][k].ToString();
  408. }
  409. }
  410. }
  411. }
  412. }
  413. }
  414. }
  415. //如果没有记录的话,将dt中含有的列的对应值设置为空
  416. else
  417. {
  418. for (int i = 0; i < ctl.Count; i++)
  419. {
  420. if (ctl[i] is TextBox || ctl[i] is Label || ctl[i] is SearchTextBox)
  421. {
  422. for (int j = 0; j < dt.Columns.Count; j++)
  423. {
  424. if (ctl[i].Name.ToUpper() == dt.Columns[j].Caption.ToUpper())
  425. {
  426. ctl[i].Text = "";
  427. }
  428. }
  429. }
  430. }
  431. }
  432. }
  433. /// <summary>
  434. /// 获取打印标签
  435. /// </summary>
  436. /// <param name="labelName"></param>
  437. /// <param name="labelUrl"></param>
  438. /// <param name="indate"></param>
  439. public static void GetPrintLabel(string labelName, string labelUrl)
  440. {
  441. BaseUtil.GetLabelUrl(labelUrl, labelName);
  442. }
  443. /// <summary>
  444. /// 获取打印标签
  445. /// </summary>
  446. /// <param name="labelName"></param>
  447. /// <param name="labelUrl"></param>
  448. /// <param name="indate"></param>
  449. public static void GetPrintLabel(string labelName, string labelUrl, string indate)
  450. {
  451. string LabelUrl = labelUrl;
  452. string LabelName = labelName;
  453. System.DateTime time = Convert.ToDateTime(indate);
  454. FileInfo file = new FileInfo(ftpOperater.DownLoadTo + LabelName);
  455. if (time.ToString() != file.LastWriteTime.ToString())
  456. BaseUtil.GetLabelUrl(LabelUrl, LabelName, time);
  457. }
  458. /// <summary>
  459. /// 获取标签的路径
  460. /// </summary>
  461. /// <param name="URL"></param>
  462. /// <param name="LabelName"></param>
  463. /// <param name="time"></param>
  464. /// <returns></returns>
  465. public static string GetLabelUrl(string URL, string LabelName, System.DateTime time)
  466. {
  467. ftpOperater ftp = new ftpOperater();
  468. return ftp.DownLoadFromSharePath(URL, LabelName);
  469. }
  470. /// <summary>
  471. /// 移除重复行
  472. /// </summary>
  473. /// <param name="dt"></param>
  474. /// <param name="field"></param>
  475. /// <returns></returns>
  476. public static DataTable DeleteSameRow(DataTable dt, string field)
  477. {
  478. ArrayList indexList = new ArrayList();
  479. // 找出待删除的行索引
  480. for (int i = 0; i < dt.Rows.Count - 1; i++)
  481. {
  482. if (!IsContain(indexList, i))
  483. {
  484. for (int j = i + 1; j < dt.Rows.Count; j++)
  485. {
  486. if (dt.Rows[i][field].ToString() == dt.Rows[j][field].ToString())
  487. {
  488. indexList.Add(j);
  489. }
  490. }
  491. }
  492. }
  493. indexList.Sort();
  494. // 排序
  495. for (int i = indexList.Count - 1; i >= 0; i--)// 根据待删除索引列表删除行
  496. {
  497. int index = Convert.ToInt32(indexList[i]);
  498. dt.Rows.RemoveAt(index);
  499. }
  500. return dt;
  501. }
  502. /// <summary>
  503. /// 判断数组中是否存在
  504. /// </summary>
  505. /// <param name="indexList">数组</param>
  506. /// <param name="index">索引</param>
  507. /// <returns></returns>
  508. public static bool IsContain(ArrayList indexList, int index)
  509. {
  510. for (int i = 0; i < indexList.Count; i++)
  511. {
  512. int tempIndex = Convert.ToInt32(indexList[i]);
  513. if (tempIndex == index)
  514. {
  515. return true;
  516. }
  517. }
  518. return false;
  519. }
  520. //播放音频文件
  521. public static void PlaySound(string FileName)
  522. {
  523. //要加载COM组件:Microsoft speech object Library
  524. if (!System.IO.File.Exists(FileName))
  525. {
  526. return;
  527. }
  528. SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
  529. SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
  530. spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
  531. SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
  532. pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
  533. spFs.Close();
  534. }
  535. /// <summary>
  536. /// 从DGV获取指定的列的数据形式是数组的形式
  537. /// </summary>
  538. public static ArrayList[] GetColumnDataFromDGV(DataGridView dgv, string[] ColumnName)
  539. {
  540. ArrayList[] array = new ArrayList[ColumnName.Length];
  541. //实例化和查询参数个数一样的ArrayList
  542. for (int i = 0; i < ColumnName.Length; i++)
  543. {
  544. array[i] = new ArrayList();
  545. }
  546. DataTable dt = (DataTable)dgv.DataSource;
  547. //如果第一列是否选框的话
  548. if (dgv.Columns[0] is DataGridViewCheckBoxColumn)
  549. {
  550. for (int i = 0; i < dt.Rows.Count; i++)
  551. {
  552. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  553. {
  554. for (int j = 0; j < ColumnName.Length; j++)
  555. {
  556. array[j].Add(dt.Rows[i][ColumnName[j]]);
  557. }
  558. }
  559. }
  560. }
  561. //否则直接获取全部的数据
  562. else
  563. {
  564. for (int i = 0; i < dgv.RowCount; i++)
  565. {
  566. for (int j = 0; j < ColumnName.Length; j++)
  567. {
  568. array[j].Add(dt.Rows[i][ColumnName[j]]);
  569. }
  570. }
  571. }
  572. return array;
  573. }
  574. /// <summary>
  575. /// 通过DataGridView和需要隐藏的字段的数组来对字段进行隐藏
  576. /// </summary>
  577. /// <param name="dgv"></param>
  578. /// <param name="field"></param>
  579. public static void HideField(DataGridView dgv, string[] field)
  580. {
  581. DataTable dt = (DataTable)dgv.DataSource;
  582. foreach (DataColumn dc in dt.Columns)
  583. {
  584. foreach (string s in field)
  585. {
  586. if (dc.Caption == s)
  587. dgv.Columns[dc.ColumnName].Visible = false;
  588. }
  589. }
  590. }
  591. /// <summary>
  592. ///
  593. /// </summary>
  594. /// <param name="dgv"></param>
  595. public static void ExpandDGVCheck(DataGridViewExpand dgv, DataGridViewCellEventArgs e)
  596. {
  597. if (e.ColumnIndex == 0 && e.RowIndex >= 0)
  598. {
  599. if (dgv.Rows[e.RowIndex] is CollapseDataGridViewRow)
  600. {
  601. int CollapseRowCount = (dgv.Rows[e.RowIndex] as CollapseDataGridViewRow).Rows.Count;
  602. if (CollapseRowCount > 0)
  603. {
  604. for (int i = (e.RowIndex + 2); i < (e.RowIndex + 1 + CollapseRowCount); i++)
  605. {
  606. try
  607. {
  608. dgv.Rows[i].Cells[0].Value = dgv.Rows[e.RowIndex].Cells[0].EditedFormattedValue;
  609. }
  610. catch (Exception) { }
  611. }
  612. }
  613. }
  614. }
  615. }
  616. /// <summary>
  617. /// 通过查询的内容获取到字段的描述
  618. /// </summary>
  619. /// <param name="field"></param>
  620. /// <returns></returns>
  621. public static string[] GetCaptionFromField(string field)
  622. {
  623. string[] caption = field.Split(',');
  624. for (int i = 0; i < caption.Length; i++)
  625. {
  626. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  627. }
  628. return caption;
  629. }
  630. /// <summary>
  631. /// 通过查询的语句获取查询的字段
  632. /// </summary>
  633. /// <param name="field"></param>
  634. /// <returns></returns>
  635. public static string[] GetField(string field)
  636. {
  637. string[] fields = field.Split(',');
  638. for (int i = 0; i < fields.Length; i++)
  639. {
  640. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  641. }
  642. return fields;
  643. }
  644. /// <summary>
  645. /// 通过描述取DataTable的列名,主要用于从配置中取数据
  646. /// </summary>
  647. /// <param name="dt"></param>
  648. /// <param name="caption"></param>
  649. /// <returns></returns>
  650. public static string GetColumnNameByCaption(DataTable dt, string caption)
  651. {
  652. foreach (DataColumn dc in dt.Columns)
  653. {
  654. if (dc.Caption.ToLower() == caption)
  655. {
  656. return dc.ColumnName;
  657. }
  658. }
  659. return null;
  660. }
  661. //用于封装异常,也可以用于错误的提示
  662. public static void ShowError(string errorMessage)
  663. {
  664. throw new Exception(errorMessage);
  665. }
  666. /// <summary>
  667. /// 判断控件的某个事件是否已经绑定了方法
  668. /// </summary>
  669. /// <param name="Control1"></param>
  670. /// <param name="EventName"></param>
  671. /// <returns></returns>
  672. public static bool ControlHasEvent(Control Control1, string EventName)
  673. {
  674. //需要查询的内容
  675. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  676. Assembly a = Assembly.GetAssembly(Control1.GetType());
  677. Type t = a.GetType(Control1.GetType().FullName, true);
  678. //获取控件的事件
  679. FieldInfo fi = t.GetField(EventName, myBindingFlags);
  680. EventHandlerList ehl = Control1.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).GetValue(Control1, null) as EventHandlerList;
  681. //判断事件的委托数量是否大于0
  682. if (ehl != null)
  683. {
  684. Delegate d = ehl[fi.GetValue(Control1)];
  685. if (d != null && d.GetInvocationList().Length > 0)
  686. {
  687. return true;
  688. }
  689. }
  690. return false;
  691. }
  692. /// <summary>
  693. /// 获取控件的事件列表
  694. /// </summary>
  695. /// <param name="Control1"></param>
  696. /// <returns></returns>
  697. public static FieldInfo[] GetControlsEvent(Control Control1)
  698. {
  699. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  700. Assembly a = Assembly.GetAssembly(Control1.GetType());
  701. Type t = a.GetType(Control1.GetType().FullName, true);
  702. FieldInfo[] finf = t.GetFields(myBindingFlags);
  703. return finf;
  704. }
  705. /// <summary>
  706. /// 清除DataTable的结构和数据,清除列结构时需要从最后的一列开始删
  707. /// </summary>
  708. /// <param name="dt"></param>
  709. public static void CleanDataTable(DataTable dt)
  710. {
  711. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  712. dt.Columns.Remove(dt.Columns[i]);
  713. }
  714. /// <summary>
  715. /// 获取标签的路径
  716. /// </summary>
  717. /// <param name="URL"></param>
  718. /// <param name="LabelName"></param>
  719. /// <returns></returns>
  720. public static string GetLabelUrl(string URL, string LabelName)
  721. {
  722. ftpOperater ftp = new ftpOperater();
  723. return ftp.DownLoadFromSharePath(URL, LabelName);
  724. }
  725. /// <summary>
  726. /// 往DataTable中添加数据
  727. /// </summary>
  728. public static void AddDataToDataTable(DataTable dt, params string[] param)
  729. {
  730. DataRow dr = dt.NewRow();
  731. for (int i = 0; i < dt.Columns.Count; i++)
  732. {
  733. dr[dt.Columns[i].ColumnName] = param[i];
  734. }
  735. }
  736. /// <summary>
  737. /// 不清除表结构,只清除数据
  738. /// </summary>
  739. /// <param name="dt"></param>
  740. public static void CleanDataTableData(DataTable dt)
  741. {
  742. for (int i = dt.Rows.Count - 1; i >= 0; i--)
  743. {
  744. dt.Rows.Remove(dt.Rows[i]);
  745. }
  746. }
  747. /// <summary>
  748. /// 获取拼接的字段
  749. /// </summary>
  750. /// <param name="dt"></param>
  751. /// <returns></returns>
  752. public static string GetFieldFromDataTable(DataTable dt)
  753. {
  754. StringBuilder sb = new StringBuilder();
  755. foreach (DataColumn dc in dt.Columns)
  756. {
  757. sb.Append(dc.Caption + ",");
  758. }
  759. return sb.ToString().Substring(sb.ToString().Length - 1, 1);
  760. }
  761. /// <summary>
  762. /// 已经定义好的DataGridView绑定数据,operate是用来添加操作列的
  763. /// </summary>
  764. /// <param name="dgv"></param>
  765. /// <param name="dt"></param>
  766. /// <param name="AddOpetateColumn"></param>
  767. /// <param name="operate"></param>
  768. public static void FillDgvWithDataTable(DataGridView dgv, DataTable dt, params DataGridViewImageColumn[] operate)
  769. {
  770. dgv.AutoGenerateColumns = false;
  771. dgv.DataSource = dt;
  772. if (operate.Length > 0)
  773. {
  774. if (dgv.Columns[operate[0].Name] != null)
  775. {
  776. dgv.Columns.Remove(dgv.Columns[operate[0].Name]);
  777. }
  778. dgv.Columns.Add(operate[0]);
  779. }
  780. ////纯英文的列不予展示
  781. //Regex regEnglish = new Regex("^[A-z]+$");
  782. //foreach (DataGridViewColumn dgvc in dgv.Columns)
  783. //{
  784. // if (regEnglish.IsMatch(dgvc.HeaderText))
  785. // {
  786. // dgvc.Visible = false;
  787. // }
  788. //}
  789. }
  790. /// <summary>
  791. /// 清除DataGridView的数据
  792. /// </summary>
  793. /// <param name="dgv"></param>
  794. public static void CleanDGVData(DataGridView dgv)
  795. {
  796. for (int i = dgv.Rows.Count - 1; i >= 0; i--)
  797. {
  798. dgv.Rows.RemoveAt(i);
  799. }
  800. DataTable dt = dgv.DataSource as DataTable;
  801. if (dt != null)
  802. {
  803. dt.AcceptChanges();
  804. }
  805. }
  806. public static void CleanForm(Form Form)
  807. {
  808. for (int i = 0; i < Form.Controls.Count; i++)
  809. {
  810. if (Form.Controls[i].Controls.Count > 0)
  811. {
  812. CleanControls(Form.Controls[i].Controls);
  813. }
  814. if (Form.Controls[i] is EnterTextBox || Form.Controls[i] is TextBox || Form.Controls[i] is ValueLabel || Form.Controls[i] is SearchTextBox || Form.Controls[i] is ValueNumLabel || Form.Controls[i] is MaCodeSearchTextBox)
  815. Form.Controls[i].Text = "";
  816. if (Form.Controls[i] is DataGridView)
  817. CleanDGVData((DataGridView)Form.Controls[i]);
  818. }
  819. }
  820. public static void CleanControls(ControlCollection collection)
  821. {
  822. for (int i = 0; i < collection.Count; i++)
  823. {
  824. if (collection[i].Controls.Count > 0)
  825. CleanControls(collection[i].Controls);
  826. if (collection[i] is EnterTextBox || collection[i] is TextBox || collection[i] is ValueLabel || collection[i] is SearchTextBox || collection[i] is ValueNumLabel || collection[i] is MaCodeSearchTextBox)
  827. collection[i].Text = "";
  828. if (collection[i] is DataGridView)
  829. CleanDGVData((DataGridView)collection[i]);
  830. }
  831. }
  832. public static void CleanControlsText(params Control[] ctl)
  833. {
  834. foreach (Control item in ctl)
  835. item.Text = "";
  836. }
  837. /// <summary>
  838. /// 需要SQL的顺序和DGV的列的顺序一致
  839. /// </summary>
  840. /// <param name="dgv"></param>
  841. /// <param name="dt"></param>
  842. /// <param name="CheckBox"></param>
  843. public static void FillExpandDgvWithDataTable(DataGridViewExpand dgv, DataTable dt, bool CheckBox, bool CheckBoxTrue)
  844. {
  845. CleanDGVData(dgv);
  846. if (CheckBox)
  847. {
  848. for (int i = 0; i < dt.Rows.Count; i++)
  849. {
  850. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  851. collapseRow.IsCollapse = true;
  852. DataGridViewCheckBoxCell checkcell = new DataGridViewCheckBoxCell();
  853. collapseRow.Cells.Add(checkcell);
  854. checkcell.Value = CheckBoxTrue;
  855. //因为DGV中可能有空置的列多出,所以需要用DataTable的列进行循环
  856. for (int j = 0; j < dt.Columns.Count; j++)
  857. {
  858. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  859. textcell.Value = dt.Rows[i][j].ToString();
  860. collapseRow.Cells.Add(textcell);
  861. textcell.ReadOnly = true;
  862. }
  863. collapseRow.Tag = "MainRow";
  864. dgv.Rows.Add(collapseRow);
  865. }
  866. }
  867. else
  868. {
  869. for (int i = 0; i < dt.Rows.Count; i++)
  870. {
  871. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  872. collapseRow.IsCollapse = true;
  873. for (int j = 1; j <= dt.Columns.Count; j++)
  874. {
  875. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  876. textcell.Value = dt.Rows[i][j - 1].ToString();
  877. collapseRow.Cells.Add(textcell);
  878. }
  879. dgv.Rows.Add(collapseRow);
  880. collapseRow.ReadOnly = true;
  881. }
  882. }
  883. }
  884. /// <summary>
  885. /// 用于给DGV中的Combox列赋静态值
  886. /// </summary>
  887. /// <param name="dgvc"></param>
  888. /// <param name="displayField"></param>
  889. /// <param name="valueField"></param>
  890. /// <param name="Value"></param>
  891. /// <returns></returns>
  892. public static void SetDgvColumnComboxData(DataGridViewComboBoxColumn dgvc, string DataPropertyName, string displayField, string valueField, string[] Value)
  893. {
  894. DataTable dt = new DataTable();
  895. dt.Columns.Add(displayField);
  896. dt.Columns.Add(valueField);
  897. for (int i = 0; i < Value.Length; i++)
  898. {
  899. DataGridViewRow row = new DataGridViewRow();
  900. dt.Rows.Add(row);
  901. dt.Rows[i][displayField] = Value[i].Split('#')[0];
  902. dt.Rows[i][valueField] = Value[i].Split('#')[1];
  903. }
  904. dgvc.DataPropertyName = DataPropertyName;
  905. dgvc.DataSource = dt;
  906. dgvc.DisplayMember = displayField;
  907. dgvc.ValueMember = valueField;
  908. }
  909. /// <summary>
  910. /// 用于给DGV中的ComboxCell赋静态值
  911. /// </summary>
  912. /// <param name="dgvcc"></param>
  913. /// <param name="displayField"></param>
  914. /// <param name="valueField"></param>
  915. /// <param name="Value"></param>
  916. public static void SetDGVCellComboxData(DataGridViewComboBoxCell dgvcc, string displayField, string valueField, string[] Value)
  917. {
  918. DataTable dt = new DataTable();
  919. dt.Columns.Add(displayField);
  920. dt.Columns.Add(valueField);
  921. for (int i = 0; i < Value.Length; i++)
  922. {
  923. DataRow dr = dt.NewRow();
  924. dr[displayField] = Value[i].Split('#')[0];
  925. dr[valueField] = Value[i].Split('#')[1];
  926. dt.Rows.Add(dr);
  927. }
  928. dgvcc.DisplayMember = displayField;
  929. dgvcc.ValueMember = valueField;
  930. dgvcc.DataSource = dt;
  931. }
  932. /// <summary>
  933. /// 获取刷选的SQL语句,传入的是TextBox的Control,传入的SQL不带Where条件
  934. /// </summary>
  935. /// <param name="SQL"></param>
  936. /// <param name="Condition"></param>
  937. /// <returns></returns>
  938. public static string GetScreenSqlCondition(params Control[] Condition)
  939. {
  940. string condition = "";
  941. //用于统计传入的控件的空值数
  942. int EmptyControlCount = 0;
  943. for (int i = 0; i < Condition.Length; i++)
  944. {
  945. //如果Text不为空再进行条件的拼接
  946. if (Condition[i].Text != "")
  947. {
  948. if (Condition[i] is ComboBox)
  949. {
  950. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' )";
  951. }
  952. else
  953. {
  954. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' )";
  955. }
  956. //如果不是最后要判断之后有没有空值的如果有一个Text的值不为空都需要添加and
  957. //添加了一次And之后跳出循环,因为如果后面多项有值会重复添加and
  958. for (int j = i + 1; j < Condition.Length; j++)
  959. {
  960. if (j < Condition.Length)
  961. {
  962. if (Condition[j].Text != "")
  963. {
  964. condition += " and ";
  965. break;
  966. }
  967. }
  968. }
  969. }
  970. else
  971. {
  972. EmptyControlCount = EmptyControlCount + 1;
  973. }
  974. }
  975. //如果所有的控件传入的都是空值则返回也为空
  976. if (EmptyControlCount == Condition.Length)
  977. return "";
  978. else
  979. condition = " where " + condition;
  980. return condition;
  981. }
  982. public static void CleanDataGridView(DataGridView dgv)
  983. {
  984. for (int i = dgv.Columns.Count - 1; i >= 0; i--)
  985. {
  986. dgv.Columns.RemoveAt(i);
  987. }
  988. }
  989. /// <summary>
  990. /// 取出SQL中的参数占位符
  991. /// </summary>
  992. /// <param name="SQL"></param>
  993. /// <returns></returns>
  994. public static string[] GetParamFromSQL(string SQL)
  995. {
  996. string[] par = SQL.Split(':');
  997. //用来存参数的数组
  998. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  999. string[] param = new string[par.Length - 1];
  1000. for (int i = 0; i < par.Length - 1; i++)
  1001. {
  1002. //新建一个char类型的数组用来存储每个字节的变量
  1003. char[] c = par[i + 1].ToCharArray();
  1004. addpar[i] = new StringBuilder();
  1005. for (int j = 0; j < c.Length; j++)
  1006. {
  1007. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  1008. {
  1009. addpar[i].Append(c[j]);
  1010. }
  1011. else
  1012. {
  1013. break;
  1014. }
  1015. }
  1016. }
  1017. for (int i = 0; i < par.Length - 1; i++)
  1018. {
  1019. param[i] = addpar[i].ToString();
  1020. }
  1021. return param;
  1022. }
  1023. public static void SetFormCenter(Form form)
  1024. {
  1025. form.StartPosition = FormStartPosition.CenterParent;
  1026. }
  1027. /// <summary>
  1028. /// 设置DataGridView的指定列可编辑
  1029. /// </summary>
  1030. /// <param name="DGV"></param>
  1031. /// <param name="EditAbleField"></param>
  1032. public static void SetDataGridViewReadOnly(DataGridView DGV, string[] EditAbleField)
  1033. {
  1034. foreach (DataGridViewColumn dc in DGV.Columns)
  1035. {
  1036. dc.ReadOnly = true;
  1037. foreach (string s in EditAbleField)
  1038. {
  1039. if (dc.Name.ToLower() == s.ToLower())
  1040. {
  1041. DGV.Columns[dc.Name].ReadOnly = false;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. //判断带有CheckBox的DGV是否有项目勾选了
  1047. public static DataTable DGVIfChecked(DataGridView dgv)
  1048. {
  1049. int CheckCount = 0;
  1050. DataTable dt = new DataTable();
  1051. //第一列是勾选框,排除在循环之外
  1052. for (int i = 1; i < dgv.Columns.Count; i++)
  1053. {
  1054. dt.Columns.Add(dgv.Columns[i].Name);
  1055. }
  1056. for (int i = 0; i < dgv.RowCount; i++)
  1057. {
  1058. if (dgv.Rows[i].Cells[0].Value != null)
  1059. {
  1060. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  1061. {
  1062. if (dgv.Rows[i].Tag.ToString() == "SonRow")
  1063. {
  1064. DataRow dr = dt.NewRow();
  1065. for (int j = 1; j < dgv.ColumnCount; j++)
  1066. {
  1067. dr[dgv.Columns[j].Name] = dgv.Rows[i].Cells[j].FormattedValue;
  1068. }
  1069. dt.Rows.Add(dr);
  1070. CheckCount++;
  1071. }
  1072. }
  1073. }
  1074. }
  1075. //判断是否勾选了明细
  1076. if (CheckCount == 0)
  1077. return null;
  1078. return dt;
  1079. }
  1080. public static void GetExpandDGVCheckedRow(DataGridView dgv, DataTable dt, int RowIndex, int DistinctColumnIndex)
  1081. {
  1082. //第一列是勾选框,排除在循环之外
  1083. if (dt.Columns.Count == 0)
  1084. {
  1085. for (int i = 0; i < dgv.Columns.Count; i++)
  1086. dt.Columns.Add(dgv.Columns[i].Name);
  1087. }
  1088. //是展开的子行的数据
  1089. if (dgv.Rows[RowIndex].Tag != null && dgv.Rows[RowIndex].Tag.ToString() == "SonRow")
  1090. {
  1091. DataRow dr = dt.NewRow();
  1092. DataRow[] datarow = (dt.Select(dgv.Columns[DistinctColumnIndex].Name + " ='" + dgv.Rows[RowIndex].Cells[DistinctColumnIndex].FormattedValue + "'"));
  1093. //判断值是否存在,存在移除重新添加
  1094. if (datarow.Length > 0)
  1095. {
  1096. dt.Rows.Remove(datarow[0]);
  1097. }
  1098. for (int j = 0; j < dgv.ColumnCount; j++)
  1099. {
  1100. dr[dgv.Columns[j].Name] = dgv.Rows[RowIndex].Cells[j].FormattedValue;
  1101. }
  1102. dt.Rows.Add(dr);
  1103. }
  1104. }
  1105. /// <summary>
  1106. /// 设置只允许输入数字
  1107. /// </summary>
  1108. /// <param name="sender"></param>
  1109. /// <param name="e"></param>
  1110. public static void NumOnly(object sender, KeyPressEventArgs e)
  1111. {
  1112. if (e.KeyChar != '\b')//这是允许输入退格键
  1113. {
  1114. if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
  1115. {
  1116. e.Handled = true;
  1117. }
  1118. }
  1119. }
  1120. public static string AddField(string[] Fields)
  1121. {
  1122. string sql = " ";
  1123. foreach (string field in Fields)
  1124. sql += field + ",";
  1125. return sql.Substring(0, sql.Length - 1);
  1126. }
  1127. /// <summary>
  1128. /// 筛选DataTable
  1129. /// </summary>
  1130. /// <param name="dt"></param>
  1131. /// <param name="condition"></param>
  1132. /// <returns></returns>
  1133. public static DataTable filterDataTable(DataTable dt, String condition)
  1134. {
  1135. if (dt == null)
  1136. return new DataTable();
  1137. //获取筛选条件中的列名,值
  1138. DataRow[] dataRows = dt.Select(condition);
  1139. DataTable ndt = dt.Clone();
  1140. for (int i = 0; i < dataRows.Length; i++)
  1141. {
  1142. ndt.Rows.Add(dataRows[i].ItemArray);
  1143. }
  1144. return ndt;
  1145. }
  1146. /// <summary>
  1147. /// 图表绘制公共方法样本
  1148. /// </summary>
  1149. /// <param name="chart1"></param>
  1150. /// <param name="_dt"></param>
  1151. /// <param name="seriesChartType"></param>
  1152. /// <param name="_title"></param>
  1153. /// <param name="XValueMember"></param>
  1154. /// <param name="YValueMembers"></param>
  1155. /// <param name="Xname"></param>
  1156. /// <param name="Yname"></param>
  1157. public static void ViewChart(Chart chart1, DataTable _dt, SeriesChartType seriesChartType, string _title, string XValueMember, string YValueMembers, string Xname, string Yname)
  1158. {
  1159. chart1.Series[0].ChartType = seriesChartType;
  1160. chart1.DataSource = _dt;
  1161. chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
  1162. chart1.Series[0].MarkerSize = 8;
  1163. chart1.Series[0].XValueMember = XValueMember;
  1164. chart1.Series[0].YValueMembers = YValueMembers;
  1165. chart1.Series[0].Label = "#VAL";
  1166. chart1.Series[0].LabelToolTip = Xname + ": #VAL\r\n " + Yname + " #VALX";
  1167. chart1.Series[0].BackSecondaryColor = Color.DarkCyan;
  1168. chart1.Series[0].BorderColor = Color.DarkOliveGreen;
  1169. chart1.Series[0].LabelBackColor = Color.Transparent;
  1170. chart1.Series[0].LegendText = Yname;
  1171. chart1.Legends[0].Title = _title;
  1172. //chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
  1173. }
  1174. /// <summary>
  1175. /// 将新增打印进程信息写入静态文件
  1176. /// </summary>
  1177. /// <param name="lbl"></param>
  1178. public static void WriteLbl(ApplicationClass lbl)
  1179. {
  1180. Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  1181. Dictionary<string, int> ProInf = new Dictionary<string, int>();
  1182. for (int i = 0; i < processes.Length; i++)
  1183. {
  1184. try
  1185. {
  1186. if (processes[i].ProcessName == "lppa")
  1187. {
  1188. ProInf.Add(processes[i].StartTime.ToString(), processes[i].Id);
  1189. }
  1190. }
  1191. catch
  1192. {
  1193. }
  1194. }
  1195. var temp = ProInf.Keys.Max();
  1196. String str = SystemInf.ProcessesID + "|" + ProInf[temp];
  1197. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Append, FileAccess.Write);
  1198. StreamWriter sw = new StreamWriter(fs);
  1199. sw.WriteLine(str, Encoding.UTF8);
  1200. sw.Close();
  1201. fs.Close();
  1202. }
  1203. public static void WriteLbl()
  1204. {
  1205. //Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  1206. //Dictionary<string, int> ProInf = new Dictionary<string, int>();
  1207. //for (int i = 0; i < processes.Length; i++)
  1208. //{
  1209. // try
  1210. // {
  1211. // if (processes[i].ProcessName == "lppa")
  1212. // {
  1213. // ProInf.Add(processes[i].StartTime.ToString(), processes[i].Id);
  1214. // }
  1215. // }
  1216. // catch
  1217. // {
  1218. // }
  1219. //}
  1220. //var temp = ProInf.Keys.Max();
  1221. //String str = SystemInf.ProcessesID + "|" + ProInf[temp];
  1222. //FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Append, FileAccess.Write);
  1223. //StreamWriter sw = new StreamWriter(fs);
  1224. //sw.WriteLine(str, Encoding.UTF8);
  1225. //sw.Close();
  1226. //fs.Close();
  1227. }
  1228. public static Object GetCacheData(string ParamName)
  1229. {
  1230. try
  1231. {
  1232. Object o = null;
  1233. //根据地址读取xml文件
  1234. XmlDocument doc = new XmlDocument();
  1235. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  1236. //忽略文档里面的注释
  1237. settings.IgnoreComments = true;
  1238. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  1239. doc.Load(reader);
  1240. //先得到根节点
  1241. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  1242. //再由根节点去找制定的节点
  1243. XmlNodeList nodeList = rootNode.ChildNodes;
  1244. foreach (XmlNode node in nodeList)
  1245. {
  1246. //找到了这个节点名字
  1247. if (node.Name == ParamName)
  1248. {
  1249. //返回节点的内容
  1250. switch (((XmlElement)node).GetAttribute("Type"))
  1251. {
  1252. case "System.String":
  1253. o = node.InnerText;
  1254. break;
  1255. case "System.Int32":
  1256. o = int.Parse(node.InnerText);
  1257. break;
  1258. case "System.Boolean":
  1259. o = node.InnerText == "True" ? true : false;
  1260. break;
  1261. default:
  1262. break;
  1263. }
  1264. break;
  1265. }
  1266. }
  1267. //关闭reader
  1268. reader.Close();
  1269. if (o == null)
  1270. return "";
  1271. else
  1272. return o;
  1273. }
  1274. catch (Exception e)
  1275. {
  1276. LogManager.DoLog(e.Message);
  1277. return "";
  1278. }
  1279. }
  1280. public static void SetCacheData(string ParamName, object Value)
  1281. {
  1282. try
  1283. {
  1284. //根据地址读取xml文件
  1285. XmlDocument doc = new XmlDocument();
  1286. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  1287. //忽略文档里面的注释
  1288. settings.IgnoreComments = true;
  1289. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  1290. doc.Load(reader);
  1291. //先得到根节点
  1292. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  1293. //再由根节点去找制定的节点
  1294. XmlNodeList nodeList = rootNode.ChildNodes;
  1295. bool flag = false;
  1296. foreach (XmlNode node in nodeList)
  1297. {
  1298. //找到了这个节点名字
  1299. if (node.Name == ParamName)
  1300. {
  1301. //就直接赋值
  1302. node.InnerText = Value.ToString();
  1303. flag = true;
  1304. }
  1305. }
  1306. //如果没有该节点,就创建节点保存结果
  1307. if (!flag)
  1308. {
  1309. //创建节点
  1310. XmlElement newNode = doc.CreateElement(ParamName);
  1311. XmlAttribute attr = doc.CreateAttribute("Type");
  1312. attr.InnerText = Value.GetType().ToString();
  1313. newNode.InnerText = Value.ToString();
  1314. newNode.SetAttributeNode(attr);
  1315. //讲新建的节点挂到根节点上
  1316. rootNode.AppendChild(newNode);
  1317. }
  1318. //关闭Reader
  1319. reader.Close();
  1320. doc.Save(SystemInf.CacheFilePath);
  1321. }
  1322. catch (Exception e)
  1323. {
  1324. LogManager.DoLog(e.Message);
  1325. }
  1326. }
  1327. public static void ClosePrint(ApplicationClass lbl)
  1328. {
  1329. lblpro = lbl;
  1330. Thread close = new Thread(ClosePrintProcess);
  1331. close.Start();
  1332. }
  1333. public static void ClosePrint(Engine lbl)
  1334. {
  1335. eng = lbl;
  1336. Thread close = new Thread(ClosePrintProcess);
  1337. close.Start();
  1338. }
  1339. static Engine eng;
  1340. static ApplicationClass lblpro;
  1341. private static void ClosePrintProcess()
  1342. {
  1343. try
  1344. {
  1345. eng.Stop();
  1346. }
  1347. catch (Exception)
  1348. {
  1349. }
  1350. }
  1351. public static bool connectState(string path)
  1352. {
  1353. return connectState(path, "vsftpd", "vsftpd");
  1354. }
  1355. /// <summary>
  1356. /// 连接远程共享文件夹
  1357. /// </summary>
  1358. /// <param name="path">远程共享文件夹的路径</param>
  1359. /// <param name="userName">用户名</param>
  1360. /// <param name="passWord">密码</param>
  1361. /// <returns></returns>
  1362. public static bool connectState(string path, string userName, string passWord)
  1363. {
  1364. bool Flag = false;
  1365. Process proc = new Process();
  1366. try
  1367. {
  1368. proc.StartInfo.FileName = "cmd.exe";
  1369. proc.StartInfo.UseShellExecute = false;
  1370. proc.StartInfo.RedirectStandardInput = true;
  1371. proc.StartInfo.RedirectStandardOutput = true;
  1372. proc.StartInfo.RedirectStandardError = true;
  1373. proc.StartInfo.CreateNoWindow = true;
  1374. proc.Start();
  1375. string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
  1376. proc.StandardInput.WriteLine(dosLine);
  1377. proc.StandardInput.WriteLine("exit");
  1378. while (!proc.HasExited)
  1379. {
  1380. proc.WaitForExit(1);
  1381. }
  1382. string errormsg = proc.StandardError.ReadToEnd();
  1383. proc.StandardError.Close();
  1384. if (string.IsNullOrEmpty(errormsg))
  1385. {
  1386. Flag = true;
  1387. }
  1388. else
  1389. {
  1390. throw new Exception(errormsg);
  1391. }
  1392. }
  1393. catch (Exception ex)
  1394. {
  1395. LogManager.DoLog(ex.Message);
  1396. }
  1397. finally
  1398. {
  1399. proc.Close();
  1400. proc.Dispose();
  1401. }
  1402. return Flag;
  1403. }
  1404. /// <summary>
  1405. /// 向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地
  1406. /// </summary>
  1407. /// <param name="src">要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"D:\1.avi"</param>
  1408. /// <param name="dst">保存文件的路径,不含名称及扩展名</param>
  1409. /// <param name="fileName">保存文件的名称以及扩展名</param>
  1410. public static void Transport(string src, string dst, string fileName)
  1411. {
  1412. FileStream inFileStream = new FileStream(src, FileMode.Open);
  1413. if (!Directory.Exists(dst))
  1414. {
  1415. Directory.CreateDirectory(dst);
  1416. }
  1417. dst = dst + fileName;
  1418. FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
  1419. byte[] buf = new byte[inFileStream.Length];
  1420. int byteCount;
  1421. while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
  1422. {
  1423. outFileStream.Write(buf, 0, byteCount);
  1424. }
  1425. inFileStream.Flush();
  1426. inFileStream.Close();
  1427. outFileStream.Flush();
  1428. outFileStream.Close();
  1429. }
  1430. /// <summary>
  1431. /// 取两个DataTable的交集,删除重复数据
  1432. /// </summary>
  1433. /// <param name="sourceDataTable"></param>
  1434. /// <param name="targetDataTable"></param>
  1435. /// <param name="primaryKey"></param>
  1436. /// <returns></returns>
  1437. public static DataTable DataTableMerge(DataTable sourceDataTable, DataTable targetDataTable, string primaryKey)
  1438. {
  1439. if (sourceDataTable != null || targetDataTable != null || !sourceDataTable.Equals(targetDataTable))
  1440. {
  1441. sourceDataTable.PrimaryKey = new DataColumn[] { sourceDataTable.Columns[primaryKey] };
  1442. DataTable dt = targetDataTable.Copy();
  1443. foreach (DataRow tRow in dt.Rows)
  1444. {
  1445. try
  1446. {
  1447. //拒绝自上次调用 System.Data.DataRow.AcceptChanges() 以来对该行进行的所有更改。
  1448. //因为行状态为DataRowState.Deleted时无法访问ItemArray的值
  1449. tRow.RejectChanges();
  1450. //在加载数据时关闭通知、索引维护和约束。
  1451. sourceDataTable.BeginLoadData();
  1452. //查找和更新特定行。如果找不到任何匹配行,则使用给定值创建新行。
  1453. DataRow temp = sourceDataTable.LoadDataRow(tRow.ItemArray, false);
  1454. sourceDataTable.EndLoadData();
  1455. sourceDataTable.Rows.Remove(temp);
  1456. }
  1457. catch
  1458. {
  1459. }
  1460. }
  1461. }
  1462. sourceDataTable.AcceptChanges();
  1463. return sourceDataTable;
  1464. }
  1465. //将DataRow[] 转换成DataTable
  1466. public static DataTable ToDataTable(DataRow[] rows)
  1467. {
  1468. if (rows == null || rows.Length == 0) return new DataTable();
  1469. DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
  1470. foreach (DataRow row in rows)
  1471. tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
  1472. return tmp;
  1473. }
  1474. /// <summary>
  1475. ///
  1476. /// </summary>
  1477. /// <param name="form"></param>
  1478. /// <param name="InOrOut">True表示打开窗体,False表示关闭窗体</param>
  1479. public static void FormStepInOrOut(Form form, bool InOrOut)
  1480. {
  1481. if (InOrOut)
  1482. {
  1483. for (int iNum = 0; iNum <= 10; iNum++)
  1484. {
  1485. //变更窗体的不透明度
  1486. form.Opacity = 0.1 * iNum;
  1487. //暂停
  1488. System.Threading.Thread.Sleep(20);
  1489. }
  1490. }
  1491. else
  1492. {
  1493. for (int iNum = 10; iNum >= 0; iNum--)
  1494. {
  1495. //变更窗体的不透明度
  1496. form.Opacity = 0.1 * iNum;
  1497. //暂停
  1498. System.Threading.Thread.Sleep(20);
  1499. }
  1500. }
  1501. }
  1502. public static string GetDataFromDevice(string Param)
  1503. {
  1504. String cmd = System.Windows.Forms.Application.StartupPath + "\\adb.exe";
  1505. Process p = new Process();
  1506. p.StartInfo = new System.Diagnostics.ProcessStartInfo();
  1507. p.StartInfo.FileName = cmd;//设定程序名
  1508. p.StartInfo.UseShellExecute = false; //关闭shell的使用
  1509. p.StartInfo.RedirectStandardInput = true; //重定向标准输入
  1510. p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
  1511. p.StartInfo.RedirectStandardError = true; //重定向错误输出
  1512. p.StartInfo.CreateNoWindow = true;//设置不显示窗口
  1513. string value = "";
  1514. switch (Param)
  1515. {
  1516. case "IMEI":
  1517. p.StartInfo.Arguments = " shell \n su \n service call iphonesubinfo 1";
  1518. p.Start();
  1519. try
  1520. {
  1521. p.StandardInput.Close();
  1522. string IMEI = p.StandardOutput.ReadToEnd();
  1523. foreach (System.Text.RegularExpressions.Match item in Regex.Matches(IMEI, "'\\S+"))
  1524. {
  1525. value += item.Value.Replace(".", "").Replace("'", "").Replace(")", "");
  1526. }
  1527. p.Close();
  1528. }
  1529. catch (Exception ex)
  1530. {
  1531. Console.WriteLine(ex.Message);
  1532. }
  1533. break;
  1534. case "POWER":
  1535. p.StartInfo.Arguments = " shell dumpsys battery";
  1536. p.Start();
  1537. value = Regex.Match(p.StandardOutput.ReadToEnd().ToUpper(), "LEVEL: \\d+").Value.Replace("LEVEL: ", "");
  1538. p.Close();
  1539. break;
  1540. case "MAC":
  1541. p.StartInfo.Arguments = " shell cat /sys/class/net/wlan0/address";
  1542. p.Start();
  1543. value = p.StandardOutput.ReadToEnd().Replace(":", "").ToUpper();
  1544. p.Close();
  1545. break;
  1546. case "BT":
  1547. p.StartInfo.Arguments = " shell settings get secure bluetooth_address";
  1548. p.Start();
  1549. value = p.StandardOutput.ReadToEnd().Replace(":", "").ToUpper();
  1550. p.Close();
  1551. break;
  1552. case "SPEC":
  1553. p.StartInfo.Arguments = " -d shell getprop ro.product.model";
  1554. p.Start();
  1555. value = p.StandardOutput.ReadToEnd().Replace(":", "").ToUpper();
  1556. p.Close();
  1557. break;
  1558. case "VERSION":
  1559. p.StartInfo.Arguments = " shell getprop ro.build.display.id";
  1560. p.Start();
  1561. value = p.StandardOutput.ReadToEnd().Replace(":", "").ToUpper();
  1562. p.Close();
  1563. break;
  1564. case "CUS_VERSION":
  1565. p.StartInfo.Arguments = " shell getprop ro.elinktek.version.release";
  1566. p.Start();
  1567. value = p.StandardOutput.ReadToEnd().Replace(":", "").ToUpper();
  1568. p.Close();
  1569. break;
  1570. case "SN":
  1571. p.StartInfo.Arguments = " shell getprop ro.serialno";
  1572. p.Start();
  1573. value = p.StandardOutput.ReadToEnd().Replace(":", "").ToUpper();
  1574. p.Close();
  1575. break;
  1576. case "RESET":
  1577. p.StartInfo.Arguments = " reboot recovery";
  1578. p.Start();
  1579. value = p.StandardOutput.ReadToEnd().Replace(":", "").ToUpper();
  1580. p.Close();
  1581. break;
  1582. default:
  1583. break;
  1584. }
  1585. Console.WriteLine(Param + " : " + value.Trim());
  1586. return value.Trim();
  1587. }
  1588. public static string GetDataFromDevice(string Param, string Path)
  1589. {
  1590. String cmd = System.Windows.Forms.Application.StartupPath + "\\adb.exe";
  1591. Process p = new Process();
  1592. p.StartInfo = new System.Diagnostics.ProcessStartInfo();
  1593. p.StartInfo.FileName = cmd;//设定程序名
  1594. p.StartInfo.UseShellExecute = false; //关闭shell的使用
  1595. p.StartInfo.RedirectStandardInput = true; //重定向标准输入
  1596. p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
  1597. p.StartInfo.RedirectStandardError = true; //重定向错误输出
  1598. p.StartInfo.CreateNoWindow = true;//设置不显示窗口
  1599. string value = "";
  1600. switch (Param)
  1601. {
  1602. case "GETFILE":
  1603. p.StartInfo.Arguments = " pull " + Path;
  1604. p.Start();
  1605. value = p.StandardOutput.ReadToEnd().Replace(":", "").ToUpper();
  1606. p.Close();
  1607. break;
  1608. case "INSTALL":
  1609. p.StartInfo.Arguments = " install " + Path;
  1610. p.Start();
  1611. value = p.StandardOutput.ReadToEnd().Replace(":", "").ToUpper();
  1612. p.Close();
  1613. break;
  1614. default:
  1615. break;
  1616. }
  1617. Console.WriteLine(Param + " : " + value.Trim());
  1618. return value.Trim();
  1619. }
  1620. public static void GetWriteInfo(string FilePath, out string BARCODE, out string MAC, out string BT, out string IMEI0, out string IMEI1)
  1621. {
  1622. MAC = "";
  1623. BT = "";
  1624. IMEI0 = "";
  1625. IMEI1 = "";
  1626. BARCODE = "";
  1627. string txt = "";
  1628. while (true)
  1629. {
  1630. try
  1631. {
  1632. StreamReader sr = new StreamReader(FilePath);
  1633. while (!sr.EndOfStream)
  1634. {
  1635. string str = sr.ReadLine().ToUpper();
  1636. txt += str + "\n";
  1637. }
  1638. IMEI0 = Regex.Match(txt, "IMEI[0] = \\S[0-9]{15}\\S").Value.Replace("IMEI[", "").Replace("]", "");
  1639. IMEI1 = Regex.Match(txt, "IMEI[1] = \\S[0-9]{15}\\S").Value.Replace("IMEI[", "").Replace("]", "");
  1640. MAC = Regex.Match(txt, "WIFIADDRESS = \\[\\S+\\]").Value.Replace("WIFIADDRESS = [", "").Replace("]", "").Replace(":", "");
  1641. BT = Regex.Match(txt, "BTADDRESS = \\[\\S+\\]").Value.Replace("BTADDRESS = [", "").Replace("]", "").Replace(":", "");
  1642. BARCODE = Regex.Match(txt, "BARCODE = \\[\\S+\\]").Value.Replace("BARCODE = [", "").Replace("]", "").Replace(":", "");
  1643. if (BARCODE == "")
  1644. {
  1645. IMEI0 = Regex.Match(txt, "IMEI1\\s+'\\d+'").Value.Replace("IMEI1", "").Replace("'", "").Trim();
  1646. MAC = Regex.Match(txt, "WIFI\\s+'\\S+'").Value.Replace("WIFI", "").Replace("'", "").Trim();
  1647. BT = Regex.Match(txt, "BT\\s+'\\S+'").Value.Replace("BT", "").Replace("'", "").Trim();
  1648. BARCODE = Regex.Match(txt, "SN1\\s+'\\S+'").Value.Replace("SN1", "").Replace("'", "").Trim();
  1649. }
  1650. break;
  1651. }
  1652. catch (Exception e)
  1653. {
  1654. Console.WriteLine(e.Message);
  1655. Thread.Sleep(1000);
  1656. }
  1657. }
  1658. }
  1659. public static bool OpenCSVFile(ref DataTable mycsvdt, string filepath)
  1660. {
  1661. string strpath = filepath; //csv文件的路径
  1662.             try
  1663. {
  1664. int intColCount = 0;
  1665. bool blnFlag = true;
  1666. DataColumn mydc;
  1667. DataRow mydr;
  1668. string strline;
  1669. string[] aryline;
  1670. StreamReader mysr = new StreamReader(strpath, System.Text.Encoding.UTF8);
  1671. while ((strline = mysr.ReadLine()) != null)
  1672. {
  1673. aryline = strline.Split(new char[] { ',' });
  1674. //给datatable加上列名
  1675. if (blnFlag)
  1676. {
  1677. blnFlag = false;
  1678. intColCount = aryline.Length;
  1679. int col = 0;
  1680. for (int i = 0; i < aryline.Length; i++)
  1681. {
  1682. col = i + 1;
  1683. mydc = new DataColumn(col.ToString());
  1684. mycsvdt.Columns.Add(mydc);
  1685. }
  1686. }
  1687. //填充数据并加入到datatable中
  1688. mydr = mycsvdt.NewRow();
  1689. for (int i = 0; i < aryline.Length; i++)
  1690. {
  1691. mydr[i] = aryline[i];
  1692. }
  1693. if (mydr[0].ToString() != "")
  1694. mycsvdt.Rows.Add(mydr);
  1695. }
  1696. mysr.Close();
  1697. return true;
  1698. }
  1699. catch (Exception ex)
  1700. {
  1701. Console.WriteLine(ex.Message + ex.StackTrace);
  1702. return false;
  1703. }
  1704. }
  1705. }
  1706. }