BaseUtil.cs 66 KB

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