BaseUtil.cs 64 KB

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