BaseUtil.cs 64 KB

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