BaseUtil.cs 65 KB

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