BaseUtil.cs 65 KB

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