BaseUtil.cs 62 KB

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