BaseUtil.cs 68 KB

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