BaseUtil.cs 61 KB

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