BaseUtil.cs 62 KB

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