BaseUtil.cs 69 KB

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