BaseUtil.cs 66 KB

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