BaseUtil.cs 55 KB

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