BaseUtil.cs 63 KB

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